LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 03: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, 03:37 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,800

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
Does this give you the behavior you expect?
Code:
shopt -s nullglob
 
1 members found this post helpful.
Old 12-17-2014, 04: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, 04:35 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,706

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

Rep: Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106
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 04:42 PM.
 
1 members found this post helpful.
Old 12-18-2014, 02: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, 02: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 05:59 PM
variable expansion in bash coolhandluke1 Programming 4 01-09-2008 03:45 PM
Variable expansion in BASH champak Programming 5 11-26-2007 02:44 AM
bash: strange problem with pathname expansion frankie_DJ Programming 2 01-14-2007 07:16 PM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:50 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