mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2091 (markdown)
25
SC2091.md
25
SC2091.md
@@ -9,6 +9,15 @@ then
|
|||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
make_command() {
|
||||||
|
printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
|
||||||
|
}
|
||||||
|
$(make_command)
|
||||||
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -18,6 +27,14 @@ then
|
|||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
make_command() {
|
||||||
|
printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
|
||||||
|
}
|
||||||
|
eval "$(make_command)"
|
||||||
|
```
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
ShellCheck has detected that you have a command that just consists of a command substitution.
|
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.
|
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:
|
### 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:
|
None.
|
||||||
```sh
|
|
||||||
readonly command_to_execute="$(print_the_command)"
|
|
||||||
$command_to_execute
|
|
||||||
```
|
|
||||||
|
|
||||||
### Related resources:
|
### Related resources:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user