Differences between revisions 1 and 280 (spanning 279 versions)
Revision 1 as of 2005-08-15 23:26:06
Size: 4610
Editor: GreyCat
Comment: Initial version
Revision 280 as of 2009-01-30 18:27:31
Size: 3686
Editor: proxy4
Comment: mammamia; http://forum.mobile-centers.com/showthread.php?p=108868#post108868 buy clomid canada; http://www.sandiegohealthdirectory.com/forum/viewtopic.php?p=19809#19809 clomid; http://www.essexandsuff
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#pragma section-numbers 2

= Bash Pitfalls =

[[TableOfContents]]

== for i in `ls *.mp3` ==

One of the most common mistakes ["BASH"] programmers make is to write a loop like this:

 {{{
 for i in `ls *.mp3`; do # Wrong!
    some command $i # Wrong!
 done
 }}}

This breaks when the user has a file with a space in its name. Why? Because the output of the `ls *.mp3` command substitution undergoes word splitting. Assuming we have a file named {{{01 - Don't Eat the Yellow Snow.mp3}}} in the current directory, the {{{for}}} loop will iterate over each word in the resulting file name (namely: "01", "-", "Don't", "Eat", and so on).

You can't double-quote the substitution either:

 {{{
 for i in "`ls *.mp3`"; do # Wrong!
 ...
 }}}

This causes the entire output of the {{{ls}}} command to be treated as a single word, and instead of iterating over each file name in the output list, the loop will only execute ''once'', with {{{i}}} taking on a value which is the concatenation of all the file names (with spaces between them).

In addition to this, the use of {{{ls}}} is just plain unnecessary. It's an external command, which simply isn't needed to do the job. So, what's the right way to do it?

 {{{
 for i in *.mp3; do # Right!
   some command "$i"
 done
 }}}

Let Bash expand the list of filenames for you. The expansion will ''not'' be subject to word splitting. Each filename that's matched by the {{{*.mp3}}} pattern will be treated as a separate word and, the loop will iterate once per file name.

The astute reader will notice the double quotes in the second line. This leads to our second common pitfall.

== cp $file $target ==

What's wrong with the command shown above? Well, nothing, '''if'' you happen to know in advance that {{{$file}}} and {{{$target}}} have no white space in them.

But if you don't know that in advance, or if you're paranoid, or if you're just trying to develop good habits, then you should quote your variable references to ''avoid'' having them undergo word splitting.

 {{{
 mv "$file" "$target"
 }}}

Without the double quotes, you'll get a command like {{{mv 01 - Don't Eat the Yellow Snow.mp3 /mnt/usb}}} and then you'll get errors like {{{mv: cannot stat `01': No such file or directory}}}. With the double quotes, all's well.

== [ $foo = "bar" ] ==

This is really the same as the previous pitfall, but I repeat it because it's ''so'' important. In the example above, the quotes are in the wrong place. You do ''not'' need to quote a string literal in bash. But you ''should'' quote your variables if you aren't sure whether they could contain white space.

 {{{
 [ "$foo" = bar ] # Right!
 }}}

Another way you could write this in bash involves the {{{[[}}} keyword, which extends and embraces the old {{{test}}} command (also known as {{{[}}}).

 {{{
 [[ $foo = bar ]] # Also right!
 }}}

You don't need to quote variable references within {{{[[ ]]}}} because they don't undergo word splitting in that context. On the other hand, quoting them won't hurt anything either.

== [ "$foo" = bar && "$bar" = foo ] ==

You can't use {{{&&}}} inside the old {{{test}}} (or {{{[}}}) command. The Bash parser sees {{{&&}}} outside of {{{[[ ]]}}} or {{{(( ))}}} and breaks your command into ''two'' commands, before and after the {{{&&}}}. Use one of these instead:

 {{{
 [ "$foo" = bar -a "$bar" = foo ] # Right!
 [ "$foo" = bar ] && [ "$bar" = foo ] # Also right!
 [[ $foo = bar && $bar = foo ]] # Also right!
 }}}

== [[ $foo > 7 ]] ==

The {{{[[ ]]}}} operator is ''not'' used for an ArithmeticExpression. It's used for strings only. If you want to do a numeric comparison against the constant 7, you must use {{{(( ))}}} instead:

 {{{
 ((foo > 7)) # Right!
 }}}

== grep foo bar | while read line; do ((count++)); done ==

The code above looks OK at first glance, doesn't it? Sure, it's just a poor implementation of {{{grep -c}}}, but it's intended as a simplistic example. So why doesn't it work? The variable {{{count}}} will be unchanged after the loop terminates, much to the surprise of Bash developers everywhere.

The reason this code does not work as expected is because each command in a pipeline is executed in a separate subshell. The changes to the {{{count}}} variable within the loop's subshell aren't reflected within the parent shell (the script in which the code occurs).

For solutions to this, please see [wiki:Self:BashFaq#faq24 Bash FAQ #24].
mammamia; http://forum.mobile-centers.com/showthread.php?p=108868#post108868 buy clomid canada; http://www.sandiegohealthdirectory.com/forum/viewtopic.php?p=19809#19809 clomid; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383715#383715 clomid uk; http://v100.vspring.com/phpBB2/viewtopic.php?p=35653#35653 clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63754#63754 clomid; http://imperialslug.com/forums/index.php?topic=2165.0 clomid; http://www.lipstitchrecords.com/forum/index.php?topic=1468.0 clomid generic; http://www.lipstitchrecords.com/forum/index.php?topic=1469.0 buy clomid online; http://sharpmeadow.se/phpBBnew//viewtopic.php?p=32210#32210 buy clomid online; http://www.project-halosis.com/EN/forum/viewtopic.php?f=8&t=15397 order clomid; http://memorybooknook.com/MBNforum/index.php?topic=1444.0 generic clomid online; http://tccarson.com/forum/viewtopic.php?p=212210#212210 uk order clomid; http://www.resolvequery.com/forum/viewtopic.php?p=182204#182204 buy clomid online; http://www.battleseen.com/community/viewtopic.php?f=25&t=13395 generic clomid usa; http://www.strikeengine.com/yourforum/viewtopic.php?p=1863#1863 order clomid; http://v100.vspring.com/phpBB2//viewtopic.php?p=35642#35642 generic clomid; http://hairyhoop.com/index.php?topic=1029.0 clomid cheap; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517253#517253 clomid; http://www.grantnewsnetwork.com/portal/modules.php?name=Forums&file=viewtopic&p=349807&sid=2160d879b387b758d0caf65ccf92ee09#349807 buy clomid online; http://tccarson.com/forum/viewtopic.php?p=212211#212211 clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63750#63750 clomid; http://www.designsforhealth.com/forum/viewtopic.php?p=7816#7816 buy cheap generic clomid online; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24567#24567 online clomid purchase,
; http://klcxgaming.com/forum/showthread.php?p=25528#post25528 clomid order; http://gofishgreatlakes.com/fishingforums/index.php?topic=23534.0 clomid; http://www.diyalcohol.com/viewtopic.php?p=257621#257621 purchase clomid; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383706#383706 buy clomid; http://purenylons.com/kinkyjacky/yabb/YaBB.pl?num=1232470514/0 order clomid; http://www.viibs.hostingarcade.com/BB/viewtopic.php?p=21698#21698 cheap clomid online; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24566#24566 clomid; http://genekeysministries.com/index.php?topic=1095.0 clomid; http://www.lipstitchrecords.com/forum/index.php?topic=1470.0 buy clomid; http://www.dnac.org/ece///////viewtopic.php?p=24902#24902 buy clomid; http://biggestforum.talkthis.com/biggestforum-post-69010.html#69010 clomid online buy; http://www.rolroyce.com/foros/viewtopic.php?p=34148#34148 clomid purchase; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383717#383717 clomid; http://fubarbreaks.co.uk/fubarbreaks/viewtopic.php?p=166117#166117 clomid order; http://www.project-halosis.com/EN/forum/viewtopic.php?f=8&t=15396 purchase generic clomid online; http://docevidarestaurante.com/forum/viewtopic.php?p=43550#43550 order cheap clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63751#63751 clomid; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517260#517260 online clomid; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24568#24568 clomid; http://provagilda.helloweb.eu/viewtopic.php?p=41833#41833 purchase clomid online; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517266#517266 clomid; http://www.viibs.hostingarcade.com/BB/viewtopic.php?p=21700#21700 clomid; http://www.javateak.co.uk/logcabins/viewtopic.php?p=6738#6738 generic clomid uk;

mammamia; http://forum.mobile-centers.com/showthread.php?p=108868#post108868 buy clomid canada; http://www.sandiegohealthdirectory.com/forum/viewtopic.php?p=19809#19809 clomid; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383715#383715 clomid uk; http://v100.vspring.com/phpBB2/viewtopic.php?p=35653#35653 clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63754#63754 clomid; http://imperialslug.com/forums/index.php?topic=2165.0 clomid; http://www.lipstitchrecords.com/forum/index.php?topic=1468.0 clomid generic; http://www.lipstitchrecords.com/forum/index.php?topic=1469.0 buy clomid online; http://sharpmeadow.se/phpBBnew//viewtopic.php?p=32210#32210 buy clomid online; http://www.project-halosis.com/EN/forum/viewtopic.php?f=8&t=15397 order clomid; http://memorybooknook.com/MBNforum/index.php?topic=1444.0 generic clomid online; http://tccarson.com/forum/viewtopic.php?p=212210#212210 uk order clomid; http://www.resolvequery.com/forum/viewtopic.php?p=182204#182204 buy clomid online; http://www.battleseen.com/community/viewtopic.php?f=25&t=13395 generic clomid usa; http://www.strikeengine.com/yourforum/viewtopic.php?p=1863#1863 order clomid; http://v100.vspring.com/phpBB2//viewtopic.php?p=35642#35642 generic clomid; http://hairyhoop.com/index.php?topic=1029.0 clomid cheap; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517253#517253 clomid; http://www.grantnewsnetwork.com/portal/modules.php?name=Forums&file=viewtopic&p=349807&sid=2160d879b387b758d0caf65ccf92ee09#349807 buy clomid online; http://tccarson.com/forum/viewtopic.php?p=212211#212211 clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63750#63750 clomid; http://www.designsforhealth.com/forum/viewtopic.php?p=7816#7816 buy cheap generic clomid online; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24567#24567 online clomid purchase, ; http://klcxgaming.com/forum/showthread.php?p=25528#post25528 clomid order; http://gofishgreatlakes.com/fishingforums/index.php?topic=23534.0 clomid; http://www.diyalcohol.com/viewtopic.php?p=257621#257621 purchase clomid; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383706#383706 buy clomid; http://purenylons.com/kinkyjacky/yabb/YaBB.pl?num=1232470514/0 order clomid; http://www.viibs.hostingarcade.com/BB/viewtopic.php?p=21698#21698 cheap clomid online; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24566#24566 clomid; http://genekeysministries.com/index.php?topic=1095.0 clomid; http://www.lipstitchrecords.com/forum/index.php?topic=1470.0 buy clomid; http://www.dnac.org/ece///////viewtopic.php?p=24902#24902 buy clomid; http://biggestforum.talkthis.com/biggestforum-post-69010.html#69010 clomid online buy; http://www.rolroyce.com/foros/viewtopic.php?p=34148#34148 clomid purchase; http://www.essexandsuffolksquash.co.uk/phpBB/viewtopic.php?p=383717#383717 clomid; http://fubarbreaks.co.uk/fubarbreaks/viewtopic.php?p=166117#166117 clomid order; http://www.project-halosis.com/EN/forum/viewtopic.php?f=8&t=15396 purchase generic clomid online; http://docevidarestaurante.com/forum/viewtopic.php?p=43550#43550 order cheap clomid; http://www.desiyell.com/phpbb/viewtopic.php?p=63751#63751 clomid; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517260#517260 online clomid; http://cubic.lnxnt.org/trolloc_forum/viewtopic.php?p=24568#24568 clomid; http://provagilda.helloweb.eu/viewtopic.php?p=41833#41833 purchase clomid online; http://www.dinero-ya.info/modules.php?name=Forums&file=viewtopic&p=517266#517266 clomid; http://www.viibs.hostingarcade.com/BB/viewtopic.php?p=21700#21700 clomid; http://www.javateak.co.uk/logcabins/viewtopic.php?p=6738#6738 generic clomid uk;

BashPitfalls (last edited 2024-04-04 23:09:24 by larryv)