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)"
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
#### Correct code:
|
||||
|
||||
```sh
|
||||
foo="$(mycmd)"
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
@@ -36,14 +36,14 @@ Shellcheck does not warn about `export foo=bar` because `bar` is a literal and n
|
||||
local foo="$(mycmd)"
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
#### Correct code:
|
||||
|
||||
```sh
|
||||
local foo
|
||||
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:
|
||||
|
||||
@@ -59,14 +59,14 @@ foo
|
||||
readonly foo="$(mycmd)"
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
#### Correct code:
|
||||
|
||||
```sh
|
||||
foo="$(mycmd)"
|
||||
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:
|
||||
```sh
|
||||
|
Reference in New Issue
Block a user