From a02310b0eddbc4ea1483fc4aaffbe975e563aeaa Mon Sep 17 00:00:00 2001 From: koalaman Date: Wed, 18 Jun 2014 09:48:50 -0700 Subject: [PATCH] Created SC2064 (markdown) --- SC2064.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 SC2064.md diff --git a/SC2064.md b/SC2064.md new file mode 100644 index 0000000..88b1401 --- /dev/null +++ b/SC2064.md @@ -0,0 +1,21 @@ +## 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. + +### Contraindications + +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. \ No newline at end of file