DEV Community

Discussion on: 5 Handy Bash Tricks in 2 Minutes

Collapse
 
learnbyexample profile image
Sundeep

Nice list.

Please please please please please use double quotes around variables, unless you explicitly desire the shell to interpret the variable value. A simple example to illustrate the issue:

$ mkdir test_shell_behavior && cd $_
$ touch 'foo' 'xyz 123'
$ for file in * ; do cp $file $file.bak; done
cp: target '123.bak' is not a directory
$ ls
foo  foo.bak  xyz 123

$ rm foo.bak
$ for file in * ; do cp "$file" "$file".bak; done
$ ls
foo  foo.bak  xyz 123  xyz 123.bak
Enter fullscreen mode Exit fullscreen mode

See this Q&A on unix.stackexchange and wooledge: Quotes for more details.


You can also use man -k instead of apropos


For testing shell globs, you can use echo

$ echo cp /some/path/to/file.txt{,.bak}
cp /some/path/to/file.txt /some/path/to/file.txt.bak
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vasilvestre profile image
Valentin Silvestre

I definitly recommand ${variable}, much better to look and feel more like you can't be wrong about it :)

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Cool! Thanks for the feedback, that is good advice. 🤠