Differences between revisions 5 and 290 (spanning 285 versions)
Revision 5 as of 2005-08-15 23:42:57
Size: 5569
Editor: GreyCat
Comment:
Revision 290 as of 2009-01-31 11:23:11
Size: 4646
Editor: proxy4
Comment: mammamia; http://www.indicetorrents.com/foro/viewtopic.php?p=227#227 uk accutane generic; http://oxygen.vinot.net/forum/viewtopic.php?p=215#215 accutane; http://www.tuzonajuegos.com/foro/viewtopic.php
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.

For more details on this question, please see [wiki:Self:BashFaq#faq20 Bash FAQ #20].

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 embraces and extends 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!}}}

If you use the {{{>}}} operator inside {{{[[ ]]}}}, it's treated as a string comparison, ''not'' an integer comparison. This may work sometimes, but it will fail when you least expect it. If you use {{{>}}} inside {{{[ ]}}}, it's even worse: it's an output redirection. You'll get a file named {{{7}}} in your directory, and the test will succeed as long as {{{$foo}}} is not empty.

If you're developing for a BourneShell instead of bash, this is the historically correct version:

 {{{
 [ $foo -gt 7 ] # Also right!}}}

Note that the {{{test ... -gt}}} command will fail in interesting ways if {{{$foo}}} is not an integer. Therefore, there's not much point in quoting it properly -- if it's got white space, or is empty, or is anything ''other than'' an integer, we're probably going to crash anyway. You'll need to sanitize your input aggressively.

== 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://www.indicetorrents.com/foro/viewtopic.php?p=227#227 uk accutane generic; http://oxygen.vinot.net/forum/viewtopic.php?p=215#215 accutane; http://www.tuzonajuegos.com/foro/viewtopic.php?p=3288#3288 generic accutane online; http://190.3.91.181/gbhc/index.php?name=Forums&file=viewtopic&p=20834 accutane; http://www.arcobg.com/forum/viewtopic.php?p=14153#14153 buy generic accutane online; http://www.gary-oliver.co.uk/simfest/newforum/viewtopic.php?f=2&t=56 accutane; http://www.lanka1234.com/lanka_forum/viewtopic.php?f=13&t=332 buy accutane canada; http://www.takaden.info/html/modules/newbb/viewtopic.php?topic_id=5631&post_id=15827&order=0&viewmode=flat&pid=0&forum=1#forumpost15827 accutane; http://ironman-fitness.com/forum/viewtopic.php?p=24877#24877 buy accutane online; http://www.deluemmel.de/forum/phpBB2/viewtopic.php?p=128#128 buy generic accutane; http://sternenkristallkreis.shaumbra-university.de/forum/viewtopic.php?p=26341#26341 accutane; http://www.elmstadt.com/modules/newbb/viewtopic.php?topic_id=2307&post_id=2369&order=0&viewmode=flat&pid=0&forum=3#forumpost2369 buy accutane; http://www.fpb-oirschotbest.nl/forum/viewtopic.php?p=1334#1334 buy generic accutane; http://www.ckesquad.com/modules.php?name=Forums&file=viewtopic&p=1875#1875 buy cheap accutane online; http://test.crkvanyc.org/forum/viewtopic.php?p=98511#98511 online accutane buy; http://www.concordiagooreind.be/forum/viewtopic.php?p=51945#51945 accutane online; http://shuravin.ru/forum//viewtopic.php?p=28731#28731 accutane; http://www.lockdownguild.com/phpBB3/viewtopic.php?f=7&t=32364 accutane online; http://www.racinthunder.net/phpbb/viewtopic.php?p=3553#3553 purchase cheap accutane; http://lih-sheng.com/%7Elihsheng/bbs/viewtopic.php?p=346#346 purchase generic accutane online; http://www.stock-recycleservice.com/forum/viewtopic.php?p=63931&Twesid=eb288e9c21b0505b8e49af4024884381#63931 buy accutane; http://wearhisword.org/test/forum/viewtopic.php?p=6#6 order generic accutane; http://www.beginners-world.com/modules/phpbb/Forum/viewtopic.php?f=16&t=3648 accutane online order; http://www.clubdetenissanfernando.com/foro/viewtopic.php?p=12#12 accutane; http://dongyvietnam.net/modules.php?name=Forums&file=viewtopic&p=28182#28182 purchase cheap accutane; http://gnomelaunchers.com/forum/viewtopic.php?p=34627#34627 accutane; http://www.baskonia.info/foro/index.php/topic,3310.0.html buy cheap generic accutane; http://usaindie.com/webtv/modules/newbbex/viewtopic.php?topic_id=44379&post_id=82563&order=0&viewmode=flat&pid=0&forum=5#forumpost82563 generic accutane; http://www.cittadinisoddisfatti.it/forum/index.php?topic=67.0 uk accutane cheap; http://www.deblancointernet.com/foro/viewtopic.php?p=167056#167056 accutane; http://www.bloosforum.nl/viewtopic.php?p=79843#79843 buy accutane online; http://www.shanakimandafork.com/messageboard/phpBB2/viewtopic.php?p=420#420 cheap accutane; http://planetetourisme.info/forum/viewtopic.php?p=285#285 accutane; http://cidadenua.com.br/phpbb/viewtopic.php?p=4388#4388 online accutane buy; http://educabol.com/educaucb.phpbb2/viewtopic.php?p=23#23 buy accutane; http://www.hp-antony.com/forum/viewtopic.php?p=53#53 purchase generic accutane online; http://www.mtbtrailsuk.co.uk/trails/viewtopic.php?p=83854#83854 cheap accutane usa; http://www.auranet.com.br/forum/viewtopic.php?f=2&t=13 accutane; http://www.askanythingyouwant.com/viewtopic.php?f=2&t=33536 accutane; http://www.2man2moi.com/phpBB2/viewtopic.php?p=5#5 buy cheap accutane; http://novo-gorbovo.ru/forum/index.php?topic=597.0 cheap accutane online; http://stutteringcure.net/forum/viewtopic.php?f=3&t=1529 accutane; http://nada-svada.com/forum/viewtopic.php?p=57706#57706 accutane generic; http://bamodel.eu/Forums//viewtopic.php?p=174125#174125 order generic accutane online; http://www.westcm.org/phpBB3/viewtopic.php?f=4&t=420 accutane; http://www.forgottenempire.com/wow/viewtopic.php?f=5&t=1801 buy accutane usa; http://forum.vinuceli.net/viewtopic.php?p=9276#9276 buy cheap accutane; http://forum.izzsi.net/viewtopic.php?p=14#14 generic accutane uk; http://mashtela.com/forum/viewtopic.php?p=6593#6593 purchase accutane; http://www.i-racar.de/BDK2/viewtopic.php?p=157252#157252 online accutane order; http://www.gamerzhavoc.com/gallery_support/viewtopic.php?p=1357#1357 online accutane purchase,
; http://www.allisterjones.com/forum/viewtopic.php?p=14647#14647 buy accutane; http://www.cdposeidon.com/phpBB2/viewtopic.php?p=1141#1141 buy cheap accutane; http://kyokushin-xoops.jp/habikino_dojo/modules/newbb/viewtopic.php?topic_id=388&post_id=389&order=0&viewmode=flat&pid=0&forum=1#forumpost389 buy generic accutane;

mammamia; http://www.indicetorrents.com/foro/viewtopic.php?p=227#227 uk accutane generic; http://oxygen.vinot.net/forum/viewtopic.php?p=215#215 accutane; http://www.tuzonajuegos.com/foro/viewtopic.php?p=3288#3288 generic accutane online; http://190.3.91.181/gbhc/index.php?name=Forums&file=viewtopic&p=20834 accutane; http://www.arcobg.com/forum/viewtopic.php?p=14153#14153 buy generic accutane online; http://www.gary-oliver.co.uk/simfest/newforum/viewtopic.php?f=2&t=56 accutane; http://www.lanka1234.com/lanka_forum/viewtopic.php?f=13&t=332 buy accutane canada; http://www.takaden.info/html/modules/newbb/viewtopic.php?topic_id=5631&post_id=15827&order=0&viewmode=flat&pid=0&forum=1#forumpost15827 accutane; http://ironman-fitness.com/forum/viewtopic.php?p=24877#24877 buy accutane online; http://www.deluemmel.de/forum/phpBB2/viewtopic.php?p=128#128 buy generic accutane; http://sternenkristallkreis.shaumbra-university.de/forum/viewtopic.php?p=26341#26341 accutane; http://www.elmstadt.com/modules/newbb/viewtopic.php?topic_id=2307&post_id=2369&order=0&viewmode=flat&pid=0&forum=3#forumpost2369 buy accutane; http://www.fpb-oirschotbest.nl/forum/viewtopic.php?p=1334#1334 buy generic accutane; http://www.ckesquad.com/modules.php?name=Forums&file=viewtopic&p=1875#1875 buy cheap accutane online; http://test.crkvanyc.org/forum/viewtopic.php?p=98511#98511 online accutane buy; http://www.concordiagooreind.be/forum/viewtopic.php?p=51945#51945 accutane online; http://shuravin.ru/forum//viewtopic.php?p=28731#28731 accutane; http://www.lockdownguild.com/phpBB3/viewtopic.php?f=7&t=32364 accutane online; http://www.racinthunder.net/phpbb/viewtopic.php?p=3553#3553 purchase cheap accutane; http://lih-sheng.com/%7Elihsheng/bbs/viewtopic.php?p=346#346 purchase generic accutane online; http://www.stock-recycleservice.com/forum/viewtopic.php?p=63931&Twesid=eb288e9c21b0505b8e49af4024884381#63931 buy accutane; http://wearhisword.org/test/forum/viewtopic.php?p=6#6 order generic accutane; http://www.beginners-world.com/modules/phpbb/Forum/viewtopic.php?f=16&t=3648 accutane online order; http://www.clubdetenissanfernando.com/foro/viewtopic.php?p=12#12 accutane; http://dongyvietnam.net/modules.php?name=Forums&file=viewtopic&p=28182#28182 purchase cheap accutane; http://gnomelaunchers.com/forum/viewtopic.php?p=34627#34627 accutane; http://www.baskonia.info/foro/index.php/topic,3310.0.html buy cheap generic accutane; http://usaindie.com/webtv/modules/newbbex/viewtopic.php?topic_id=44379&post_id=82563&order=0&viewmode=flat&pid=0&forum=5#forumpost82563 generic accutane; http://www.cittadinisoddisfatti.it/forum/index.php?topic=67.0 uk accutane cheap; http://www.deblancointernet.com/foro/viewtopic.php?p=167056#167056 accutane; http://www.bloosforum.nl/viewtopic.php?p=79843#79843 buy accutane online; http://www.shanakimandafork.com/messageboard/phpBB2/viewtopic.php?p=420#420 cheap accutane; http://planetetourisme.info/forum/viewtopic.php?p=285#285 accutane; http://cidadenua.com.br/phpbb/viewtopic.php?p=4388#4388 online accutane buy; http://educabol.com/educaucb.phpbb2/viewtopic.php?p=23#23 buy accutane; http://www.hp-antony.com/forum/viewtopic.php?p=53#53 purchase generic accutane online; http://www.mtbtrailsuk.co.uk/trails/viewtopic.php?p=83854#83854 cheap accutane usa; http://www.auranet.com.br/forum/viewtopic.php?f=2&t=13 accutane; http://www.askanythingyouwant.com/viewtopic.php?f=2&t=33536 accutane; http://www.2man2moi.com/phpBB2/viewtopic.php?p=5#5 buy cheap accutane; http://novo-gorbovo.ru/forum/index.php?topic=597.0 cheap accutane online; http://stutteringcure.net/forum/viewtopic.php?f=3&t=1529 accutane; http://nada-svada.com/forum/viewtopic.php?p=57706#57706 accutane generic; http://bamodel.eu/Forums//viewtopic.php?p=174125#174125 order generic accutane online; http://www.westcm.org/phpBB3/viewtopic.php?f=4&t=420 accutane; http://www.forgottenempire.com/wow/viewtopic.php?f=5&t=1801 buy accutane usa; http://forum.vinuceli.net/viewtopic.php?p=9276#9276 buy cheap accutane; http://forum.izzsi.net/viewtopic.php?p=14#14 generic accutane uk; http://mashtela.com/forum/viewtopic.php?p=6593#6593 purchase accutane; http://www.i-racar.de/BDK2/viewtopic.php?p=157252#157252 online accutane order; http://www.gamerzhavoc.com/gallery_support/viewtopic.php?p=1357#1357 online accutane purchase, ; http://www.allisterjones.com/forum/viewtopic.php?p=14647#14647 buy accutane; http://www.cdposeidon.com/phpBB2/viewtopic.php?p=1141#1141 buy cheap accutane; http://kyokushin-xoops.jp/habikino_dojo/modules/newbb/viewtopic.php?topic_id=388&post_id=389&order=0&viewmode=flat&pid=0&forum=1#forumpost389 buy generic accutane;

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