From ba6b9aa038ec994042bc9c6d17d6f0216dee99cc Mon Sep 17 00:00:00 2001 From: Will Holen <46539685+willholen@users.noreply.github.com> Date: Tue, 8 Sep 2020 16:02:05 -0700 Subject: [PATCH] Updated SC2091 (markdown) --- SC2091.md | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/SC2091.md b/SC2091.md index 45e930b..e6d8059 100644 --- a/SC2091.md +++ b/SC2091.md @@ -9,6 +9,15 @@ then fi ``` +or + +```sh +make_command() { + printf 'cat header %q footer > %q\n' "$1" "$2" | tee log +} +$(make_command) +``` + ### Correct code: ```sh @@ -18,6 +27,14 @@ then fi ``` +or + +```sh +make_command() { + printf 'cat header %q footer > %q\n' "$1" "$2" | tee log +} +eval "$(make_command)" +``` ### Rationale: ShellCheck has detected that you have a command that just consists of a command substitution. @@ -32,13 +49,11 @@ Sometimes this results in this confounding `command not found` messages. Other t The solution is simply to remove the surrounding `$()`. This will execute the command instead of the command's output. +If you instead have based a script around generating shell command strings, you can use `eval "$(cmd)"` to evaluate the output of a command as a second command. The benefit of using `eval` is that quotes, pipes, redirections, and other shell constructs will work as expected. Without the `eval`, all these will be treated as literal strings instead. + ### Exceptions: -If you really want to execute the output of a command rather than the command itself, you can ignore this message or assign the output to a new variable first: -```sh -readonly command_to_execute="$(print_the_command)" -$command_to_execute -``` +None. ### Related resources: