Register
It is currently Sun Oct 26, 2014 1:43 am

strange echo behaviour


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Oct 25, 2010 4:47 am   

Joined: Sun Oct 24, 2010 7:36 am
Posts: 2
Hello,

I ran into problem - that I solved, but still not understand ;) Maybe some of you guys can help me.

Code:
$ echo [iptables] asdf
a asdf


I found out that there was a file called "a". When I removed the letter "a" from "iptables" or when I deleted this file, the output was "[iptables] asdf":

Code:
$ echo [iptbles] asdf
[iptbles] asdf

$ rm a
$ echo [iptables] asdf
[iptables] asdf


Ok.. so when I use a letter in brackets, and a file with this letter exists, output won't be correct.

I played a bit arround:

file "a" exists:

Code:
$ echo [iptables] asdf
a asdf

$ echo [iptables foo] asdf
[iptables foo] asdf


If there are two words in bracket, then it doesn't care if there is a file called "a".

Please, can anyone give me a hint whats the thing with brackets in the echo command??

Thanks in advance!


Top
 Profile  
 PostPosted: Mon Oct 25, 2010 11:59 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
First the global answer: filename expansion globbing. Read up on it :)
In more detail: If you use any commandline parameter without quotes, bash will try to 'fileglob' it to a filename. If it can't find it as a filename, then it fill pass it as a literal to the command in question.
So an 'echo *' will actually display all filenames in your current directory and an 'echo [abc]*' will display either all filenames starting with an a, b or c in the current directory, or if there are none the literal string '[abc]*'
Can you guess what happens with the command 'ls [abc]*' ? Hint: it's not 'ls' that does the fileglobbing! :)
Doing an 'echo "*"' will not do fileglobbing and just echo a literal '*'.
So you can either quote all commandline parameters: 'echo "I am the *best*"', or turn off fileglobbing with a 'set -f' (turn it back on with 'set +f')

[edit]
Ohw yeah, putting letters between square brackets means: take one of the following characters.
If you're breaking it up with a space, it's not a legal range anymore (unless the space is escaped) so it's taken as a literal again
[/edit]


Top
 Profile  
 PostPosted: Mon Oct 25, 2010 12:08 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Hi,

it's explained in man bash / Pathname Expansion.


Top
 Profile  
 PostPosted: Mon Oct 25, 2010 2:39 pm   

Joined: Sun Oct 24, 2010 7:36 am
Posts: 2
Thanks, that helped me a lot!!

I've already read "man echo".. but couldn't find anything there.


Top
 Profile  
 PostPosted: Mon Oct 25, 2010 4:21 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
/bin/echo for which is the echo man page, is not a bash builtin.

bash does the Pathname Expansion.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP