Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/standards/11-command-v-not-which.bad.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
#
# BAD: which is an external program with non-portable output and unreliable
# exit status. Prefer the shell builtin `command -v`.
# expect-shellcheck: SC2230
if which jq > /dev/null 2>&1; then
printf 'jq is available\n'
else
printf 'jq is not available\n'
fi
15 changes: 15 additions & 0 deletions examples/standards/11-command-v-not-which.good.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# GOOD: command -v is a shell builtin with predictable output and exit status
# for checking whether a command is available.
set -euo pipefail

main() {
if command -v jq > /dev/null; then
printf 'jq is available\n'
else
printf 'jq is not available\n'
fi
}

main "$@"
1 change: 1 addition & 0 deletions examples/standards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ paired files side by side.
| `08-brace-expansions` | `${base}_2026` | `$base_2026` (a different var) | `SC2250` |
| `09-no-parse-ls` | glob `"$dir"/*.txt` | `$(ls *.txt)` | `SC2045` |
| `10-parameter-expansion-not-sed` | `${path##*/}` strips in-process | `echo ... \| sed ...` forks twice | `SC2001` |
| `11-command-v-not-which` | `command -v jq` checks a command | `which jq` is non-standard | `SC2230` |

## `.env.example`

Expand Down
Loading