3 ms·
To illustrate why, consider a file name with spaces. filename="my file.txt" cat $filename # Incorrect: Tries to 'cat' two files: 'my' and 'file.txt'
by pjot 3y ago
To illustrate why, consider a file name with spaces.
filename="my file.txt"
cat $filename # Incorrect: Tries to 'cat' two files: 'my' and 'file.txt'
cat "$filename" # Correct: Treats 'my file.txt' as one file
- mgdlbp 3y agoAdditionally, $ f=-x $ cat "$f" cat: invalid option -- 'x' $ cat -- "$f" cat: -x: No such file or directory # ^-- correct, but: $ f=- $ cat -- "$f" reading stdin... reading stdin... ^C $ f=./- $ cat -- "$f" cat: ./-: No such file or directory ...better to glob with ./* than *
- pjot 3y agoAlso for sanitizing user input. input=$1 # User input, potentially dangerous rm $input # Incorrect: Risky if input contains something like '*' rm "$input" # Correct: Safer, treats the user input as a single item