Bash Pitfalls
Introdução
Detalhamento dos Pitfalls
1. for f in $(ls *.mp3) {#1-for-f-in-ls-mp3}
for f in $(ls *.mp3); do # Errado!
some command $f # Errado!
done2. cp $file $target {#2-cp-file-target}
3. Filenames with leading dashes {#3-filenames-with-leading-dashes}
4. [ $foo = "bar" ] {#4-foo-bar}
5. cd $(dirname "$f") {#5-cd-dirname-f}
6. [ "$foo" = bar && "$bar" = foo ] {#6-foo-bar-bar-foo}
7. [[ $foo > 7 ]] {#7-foo-7}
8. grep foo bar | while read -r; do ((count++)); done {#8-grep-while-read}
9. if [grep foo myfile] {#9-if-grep-foo-myfile}
10. if [bar="$foo"]; then … {#10-if-bar-foo}
11. if [ [ a = b ] && [ c = d ] ]; then … {#11-if-a-b-c-d}
12. read $foo {#12-read-foo}
13. cat file | sed s/foo/bar/ > file {#13-cat-sed-same-file}
14. echo $foo {#14-echo-foo}
15. $foo=bar {#15-foo-bar-assignment}
16. foo = bar {#16-foo-bar-spaces}
17. echo <<EOF {#17-echo-eof}
18. su -c 'some command' {#18-su-c-some-command}
19. cd /foo; bar {#19-cd-foo-bar}
20. [ bar == "$foo" ] {#20-bar-foo-double-equals}
21. for i in {1..10}; do ./something &; done {#21-for-i-background}
22. cmd1 && cmd2 || cmd3 {#22-cmd1-cmd2-cmd3}
23. echo "Hello World!" {#23-echo-hello-world}
24. for arg in $* {#24-for-arg-in-star}
25. function foo() {#25-function-foo}
26. echo "~" {#26-echo-tilde}
27. local var=$(cmd) {#27-local-var-cmd}
28. export foo=~/bar {#28-export-foo-tilde}
29. sed 's/$foo/good bye/' {#29-sed-variable-substitution}
30. tr [A-Z] [a-z] {#30-tr-a-z}
31. ps ax | grep gedit {#31-ps-grep}
32. printf "$foo" {#32-printf-foo}
33. for i in {1..$n} {#33-for-i-brace-expansion}
34. if [[ $foo = $bar ]] (depending on intent) {#34-pattern-matching}
35. if [[ $foo =~ 'some RE' ]] {#35-regex-quotes}
36. [ -n $foo ] or [ -z $foo ] {#36-test-n-z}
37. [[ -e "$broken_symlink" ]] returns 1 {#37-broken-symlink}
38. ed file <<<"g/d{0,3}/s//e/g" fails {#38-ed-regex}
39. expr sub-string fails for "match" {#39-expr-match}
40. On UTF-8 and Byte-Order Marks (BOM) {#40-utf8-bom}
41. content=$(<file) {#41-content-file}
42. for file in ./* ; do if [[ $file != . ]] {#42-glob-pattern}
43. somecmd 2>&1 >>logfile {#43-redirection-order}
44. cmd; (( ! $? )) || die {#44-exit-status}
45. y=$(( array[$x] )) {#45-array-arithmetic}
46. read num; echo $((num+1)) {#46-read-arithmetic}
47. IFS=, read -ra fields <<< "$csv_line" {#47-ifs-csv}
Atualizado