Use single quotes, otherwise this expands now rather than when signalled. Wontfix
trap "rm -rf '$TMPDIR'; exit" SIGHUP SIGINT SIGTERM
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use single quotes, otherwise this expands now rather than when signalled.
Problematic code:
trap "echo \"Finished on $(date)\"" EXIT
Correct code:
trap 'echo "Finished on $(date)"' EXIT
Rationale:
With double quotes, all parameter and command expansions will expand when the trap is defined rather than when it's executed.
In the example, the message will contain the date on which the trap was declared, and not the date on which the script exits.
Using single quotes will prevent expansion at declaration time, and save it for execution time.
Exceptions
If you don't care that the trap code is expanded early because the commands/variables won't change during execution of the script, or because you want to use the current and not the future values, then you can ignore this message.
Notice
Original content from the ShellCheck https://github.com/koalaman/shellcheck/wiki.
To read lines rather than words, pipe/redirect to a 'while read' loop. Open
for win in $(cat "$DATASET.wintax.regions") ; do
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
To read lines rather than words, pipe/redirect to a 'while read' loop.
Problematic code:
for line in $(cat file | grep -v '^ *#')
do
echo "Line: $line"
done
Correct code:
grep -v '^ *#' < file | while IFS= read -r line
do
echo "Line: $line"
done
or without a subshell (bash, zsh, ksh):
while IFS= read -r line
do
echo "Line: $line"
done < <(grep -v '^ *#' < file)
or without a subshell, with a pipe (more portable, but write a file on the filesystem):
mkfifo mypipe
grep -v '^ *#' < file > mypipe &
while IFS= read -r line
do
echo "Line: $line"
done < mypipe
rm mypipe
Rationale:
For loops by default (subject to $IFS
) read word by word. Additionally, glob expansion will occur.
Given this text file:
foo *
bar
The for loop will print:
Line: foo
Line: aardwark.jpg
Line: bullfrog.jpg
...
The while loop will print:
Line: foo *
Line: bar
Exceptions
If you want to read word by word, you should still use a while read loop (e.g. with read -a
to read words into an array).
Rare reasons for ignoring this message is if you don't care because your file only contains numbers and you're not interested in good practices, or if you've set $IFS
appropriately and also disabled globbing.
Notice
Original content from the ShellCheck https://github.com/koalaman/shellcheck/wiki.