From 79541f56f829fbadfbf3dbd39f079f9a80f35607 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 17 Dec 2016 15:31:52 -0800 Subject: [PATCH] Created SC2188 (markdown) --- SC2188.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC2188.md diff --git a/SC2188.md b/SC2188.md new file mode 100644 index 0000000..056400c --- /dev/null +++ b/SC2188.md @@ -0,0 +1,33 @@ +## This redirection doesn't have a command. Move to its command (or use 'true' as no-op). + +### Problematic code: + +```sh +{ + echo "Report for $(date +%F)" + uptime + df -h +} + > report.txt +``` + +### Correct code: + +```sh +{ + echo "Report for $(date +%F)" + uptime + df -h +} > report.txt +``` +### Rationale: + +ShellCheck found a redirection that doesn't actually redirect from/to anything. + +This could indicate a bug, such as in the problematic code where an additional linefeed causes `report.txt` to be truncated instead of containing report output, or in `foo & > bar`, where either `foo &> bar` or `foo > bar &` was intended. + +However, it could also be intentionally used to truncate a file or check that it's readable. You can make this more explicit for both ShellCheck and human readers by using `true` or `:` as a dummy command, e.g. `true > file` or `: > file`. + +### Exceptions: + +There are no semantic problems with using `> foo` over `true > foo`, so if you don't see this as a potential source of bugs or confusion, you can [[ignore]] it. \ No newline at end of file