mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2155 (markdown)
14
SC2155.md
14
SC2155.md
@@ -6,20 +6,20 @@
|
|||||||
export foo="$(mycmd)"
|
export foo="$(mycmd)"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
#### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
foo="$(mycmd)"
|
foo="$(mycmd)"
|
||||||
export foo
|
export foo
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rationale:
|
#### Rationale:
|
||||||
|
|
||||||
In the original code, the return value of `mycmd` is ignored, and `export` will instead always return true. This may prevent conditionals, `set -e` and traps from working correctly.
|
In the original code, the return value of `mycmd` is ignored, and `export` will instead always return true. This may prevent conditionals, `set -e` and traps from working correctly.
|
||||||
|
|
||||||
When first marked for export and assigned separately, the return value of the assignment will be that of `mycmd`. This avoids the problem.
|
When first marked for export and assigned separately, the return value of the assignment will be that of `mycmd`. This avoids the problem.
|
||||||
|
|
||||||
### Exceptions:
|
#### Exceptions:
|
||||||
|
|
||||||
If you intend to ignore the return value of an assignment, you can either ignore this warning or use
|
If you intend to ignore the return value of an assignment, you can either ignore this warning or use
|
||||||
|
|
||||||
@@ -36,14 +36,14 @@ Shellcheck does not warn about `export foo=bar` because `bar` is a literal and n
|
|||||||
local foo="$(mycmd)"
|
local foo="$(mycmd)"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
#### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
local foo
|
local foo
|
||||||
foo=$(mycmd)
|
foo=$(mycmd)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rationale
|
#### Rationale
|
||||||
|
|
||||||
The exit status of the command is overridden by the exit status of the creation of the local variable. For example:
|
The exit status of the command is overridden by the exit status of the creation of the local variable. For example:
|
||||||
|
|
||||||
@@ -59,14 +59,14 @@ foo
|
|||||||
readonly foo="$(mycmd)"
|
readonly foo="$(mycmd)"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
#### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
foo="$(mycmd)"
|
foo="$(mycmd)"
|
||||||
readonly foo
|
readonly foo
|
||||||
```
|
```
|
||||||
|
|
||||||
### Word splitting and quoting issue with dash, maybe others
|
#### Word splitting and quoting issue with dash, maybe others
|
||||||
|
|
||||||
A serious quoting problem with dash is another reason to declare and assign separately. Dash is the [default, `/bin/sh` shell on Ubuntu](https://wiki.ubuntu.com/DashAsBinSh). More specifically, dash version 0.5.8-2.10 and others cannot run this code:
|
A serious quoting problem with dash is another reason to declare and assign separately. Dash is the [default, `/bin/sh` shell on Ubuntu](https://wiki.ubuntu.com/DashAsBinSh). More specifically, dash version 0.5.8-2.10 and others cannot run this code:
|
||||||
```sh
|
```sh
|
||||||
|
Reference in New Issue
Block a user