LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2014, 04:14 PM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
Question bash for strange expansion


Code:
touch a b a\(c\)b
$ ls *\(c\)*
a(c)b
$ for a in *\(c\)*; do ls $a; done
a(c)b
# this is ok 
rm a\(c\)b
$ ls *\(c\)*
ls: cannot access *(c)*: No such file or directory
$ for a in *\(c\)*; do ls $a; done
a  b
# this is not clear. please tell me why would this happen?!
 
Old 12-17-2014, 04:37 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,614

Rep: Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703
Does this give you the behavior you expect?
Code:
shopt -s nullglob
 
1 members found this post helpful.
Old 12-17-2014, 05:28 PM   #3
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by smallpond View Post
Does this give you the behavior you expect?
Code:
shopt -s nullglob
Yes it does, thank you for your answer.
Why would be ls inconsistent with for, and why would *\(c\)* expand to the list of files as one word? I should be able to understand it by myself starting from here.
 
Old 12-17-2014, 05:35 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Looks like you deleted the file.
 
Old 12-17-2014, 05:40 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Put quotes around your variable in the for loop, same as you would if there's a space or any other "bad" character.

Code:
$ touch a b
for a in *\(c\)*; do ls $a; done
a  b
$ for a in *\(c\)*; do ls "$a"; done
ls: cannot access *(c)*: No such file or directory
same as this
Code:
$ touch "a b"
$ ls *\ *
a b
$ for a in *\ *; do ls $a; done
ls: cannot access a: No such file or directory
ls: cannot access b: No such file or directory
$ for a in *\ *; do ls "$a"; done
a b
If there's ever a chance you're going to have a character that requires delimiting (-, (, space, etc.) stored inside a variable, then you need to stick quotes around it when you use it.

Last edited by suicidaleggroll; 12-17-2014 at 05:42 PM.
 
1 members found this post helpful.
Old 12-18-2014, 03:34 PM   #6
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
Sorry to bother you again, but please tell me why does this happen. () should introduce some subshell which here should result in an error.
Code:
$ ls *()*
a  a(c)b  a(c)c  b
$ ls *()
ls: cannot access *(): No such file or directory
$ ls ()*
bash: syntax error near unexpected token `('
$
 
Old 12-18-2014, 03:48 PM   #7
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by doru View Post
Sorry to bother you again, but please tell me why does this happen. () should introduce some subshell which here should result in an error.
Code:
$ ls *()*
a  a(c)b  a(c)c  b
$ ls *()
ls: cannot access *(): No such file or directory
$ ls ()*
bash: syntax error near unexpected token `('
$
OK, so *() is some pattern matching in pathname expansion. So bash is looking for some file with a void name? OK, so nothing matches nothing, that's right. I did fall into a succession of traps here, did I not? Thank you all,
 
  


Reply

Tags
bash, bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash tab variable expansion veetee Linux - Software 7 01-05-2012 06:59 PM
variable expansion in bash coolhandluke1 Programming 4 01-09-2008 04:45 PM
Variable expansion in BASH champak Programming 5 11-26-2007 03:44 AM
bash: strange problem with pathname expansion frankie_DJ Programming 2 01-14-2007 08:16 PM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 12:45 PM


All times are GMT -5. The time now is 03:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration