mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Note bash temp file and posix mode caveats, add lastpipe recipes
12
SC2207.md
12
SC2207.md
@@ -11,14 +11,14 @@ array=( $(mycommand) )
|
|||||||
If it outputs multiple lines, each of which should be an element:
|
If it outputs multiple lines, each of which should be an element:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# For bash 4.x
|
# For bash 4.x, must not be in posix mode, may use temporary files
|
||||||
mapfile -t array < <(mycommand)
|
mapfile -t array < <(mycommand)
|
||||||
|
|
||||||
# For bash 3.x+
|
# For bash 3.x+, must not be in posix mode, may use temporary files
|
||||||
array=()
|
array=()
|
||||||
while IFS='' read -r line; do array+=("$line"); done < <(mycommand)
|
while IFS='' read -r line; do array+=("$line"); done < <(mycommand)
|
||||||
|
|
||||||
# For ksh
|
# For ksh, and bash 4.2+ with the lastpipe option enabled (may require disabling monitor mode)
|
||||||
array=()
|
array=()
|
||||||
mycommand | while IFS="" read -r line; do array+=("$line"); done
|
mycommand | while IFS="" read -r line; do array+=("$line"); done
|
||||||
```
|
```
|
||||||
@@ -26,9 +26,13 @@ mycommand | while IFS="" read -r line; do array+=("$line"); done
|
|||||||
If it outputs a line with multiple words (separated by spaces), other delimiters can be chosen with IFS, each of which should be an element:
|
If it outputs a line with multiple words (separated by spaces), other delimiters can be chosen with IFS, each of which should be an element:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# For bash
|
# For bash, uses temporary files
|
||||||
IFS=" " read -r -a array <<< "$(mycommand)"
|
IFS=" " read -r -a array <<< "$(mycommand)"
|
||||||
|
|
||||||
|
# For bash 4.2+ with the lastpipe option enabled (may require disabling monitor mode)
|
||||||
|
array=()
|
||||||
|
mycommand | IFS=" " read -r -a array
|
||||||
|
|
||||||
# For ksh
|
# For ksh
|
||||||
IFS=" " read -r -A array <<< "$(mycommand)"
|
IFS=" " read -r -A array <<< "$(mycommand)"
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user