LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-17-2013, 07:22 AM   #1
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Bash mini quiz.


This isn't a question but a one question quiz.

Suppose you have two directories, dir1 and dir2.
Dir1 has three files in it: song1.mp3, song2.mp3 and song3.mp3.
Dir2 has three files in it: song4.mp3, song5.mp3 and song6.mp3.

Inside dir1, enter the command "songlist=*.mp3".
"echo $songlist" displays "song1.mp3 song2.mp3 song3.mp3".

Now cd into dir2.
What does "echo $songlist" show?
song1.mp3 song2.mp3 song3.mp3
or
song4.mp3 song5.mp3 song6.mp3
 
Old 01-17-2013, 07:41 AM   #2
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Obviously song4.mp3 song5.mp3 song6.mp3... because we assigned the variable using wildcard * ;-)
 
Old 01-17-2013, 07:43 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,614
Blog Entries: 2

Rep: Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070
Not so obvious as you think. We never assigned the wildcard to the variable, it was expanded to the actual filenames by the shell before the assignment took place.
So it will show "song1.mp3 song2.mp3 song3.mp3", since this is what was assigned to the variable.
 
Old 01-17-2013, 08:00 AM   #4
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Quote:
Originally Posted by TobiSGD View Post
Not so obvious as you think. We never assigned the wildcard to the variable, it was expanded to the actual filenames by the shell before the assignment took place.
So it will show "song1.mp3 song2.mp3 song3.mp3", since this is what was assigned to the variable.
Hi TobiSGD,

Hmm!! got confused... to clear this I tested that and what I said is correct..... If you run echo $songlist from any other dir, it will show *.mp3
 
Old 01-17-2013, 08:03 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by TobiSGD View Post
Not so obvious as you think. We never assigned the wildcard to the variable, it was expanded to the actual filenames by the shell before the assignment took place.
Nope. Otherwise the resulting command line would throw a command not found error:
Code:
$ songlist=song1.mp3 song2.mp3 song3.mp3
bash: song2.mp3: command not found
There should be an exception to filename expansion in variable assignment. Diggin' into it...
 
Old 01-17-2013, 08:14 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
From the Bash reference manual:
Quote:
A variable may be assigned to by a statement of the form

name=[value]

If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (detailed below). If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((…)) expansion is not used (see Arithmetic Expansion). Word splitting is not performed, with the exception of "$@" as explained below. Filename expansion is not performed. Assignment statements may also appear as arguments to the alias, declare, typeset, export, readonly, and local builtin commands.
 
Old 01-17-2013, 08:22 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,614
Blog Entries: 2

Rep: Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070
OK, I stand corrected, bijo505 and colucix are right, I just tested it:
Code:
bash-4.2$ ls dir1 dir2
dir1:
song1.mp3  song2.mp3  song3.mp3

dir2:
song4.mp3  song5.mp3  song6.mp3
bash-4.2$ cd dir1
bash-4.2$ songlist=*.mp3
bash-4.2$ echo $songlist
song1.mp3 song2.mp3 song3.mp3
bash-4.2$ cd ../dir2
bash-4.2$ echo $songlist
song4.mp3 song5.mp3 song6.mp3
You never stop learning with Linux.

Fun fact: I tried this first in Zsh, this is what I got:
Code:
tobi dir1 ☺ $ songlist=*.mp3
tobi dir1 ☺ $ echo $songlist
*.mp3
 
Old 01-17-2013, 09:42 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In zsh:
Code:
$ setopt globassign
$ songlist=*.mp3
$ echo $songlist
song1.mp3 song2.mp3 song3.mp3
In this case it is the actual and only value of the variable, so that moving to Dir2 the value is the same. On the contrary if you assign the option globsubst, you get the same behaviour described previously, since the filename expansion is performed upon the variable evaluation (not upon the assignment).
 
1 members found this post helpful.
Old 01-17-2013, 09:52 AM   #9
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,614
Blog Entries: 2

Rep: Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070
Thanks for the clarification.
 
Old 01-17-2013, 09:15 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
I tested it in bash (default settings on Centos6) and got the list of files for whichever dir I was in.
IOW, the effect is that it 'stores' the wildcard string, but when you 'echo' the var, it effectively 'eval's it and matches the current dir content.

I must admit originally I guessed it would store the first input: wrong
 
Old 01-17-2013, 11:37 PM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Original Poster
Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Since arguments are evaluated by bash before a command runs, I was surprised when I noticed this late binding. I was doing something like "for file in ${filelist}; ..."

If you export the variable and run "env | grep songlist" the result is *.mp3.

Now for extra credit, try "songlist=(*.mp3)"
You can use "echo ${songlist[*]}" to display the contents of a bash array.
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Avoiding Linux Installation Problems on the HP Mini 110 and Mini 210 Netbooks LXer Syndicated Linux News 0 06-13-2010 11:10 PM
ASUS Eee mini laptop- how to get a (bash) shell running? vous Linux - Hardware 1 06-19-2008 05:06 AM
New Quiz? neilcpp LQ Suggestions & Feedback 9 03-08-2004 10:51 PM
New Quiz - bash jeremy Programming 2 01-09-2003 12:23 AM
quiz nautilus_1987 LQ Suggestions & Feedback 12 09-21-2002 10:35 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration