Bash Pitfalls
This page shows common errors that Bash programmers make. The following examples are all flawed in some way:
Contents
- for i in `ls *.mp3`
- cp $file $target
- Filenames with leading dashes
- [ $foo = “bar” ]
- cd `dirname “$f”`
- [ “$foo” = bar && “$bar” = foo ]
- [[ $foo > 7 ]]
- grep foo bar | while read line; do ((count++)); done
- if [grep foo myfile]
- if [bar=”$foo”]
- if [ [ a = b ] && [ c = d ] ]
- cat file | sed s/foo/bar/ > file
- echo $foo
- $foo=bar
- foo = bar
- echo <<EOF
- su -c ‘some command’
- cd /foo; bar
- [ bar == “$foo” ]
- for i in {1..10}; do ./something &; done
- cmd1 && cmd2 || cmd3
- On UTF-8 and Byte-Order Marks (BOM)
- echo “Hello World!”
- for arg in $*
- function foo()
- echo “~”
- local varname=$(command)
- foo=”hello”; sed -e ‘s/$foo/good bye/’
- tr [A-Z] [a-z]