Note bash temp file and posix mode caveats, add lastpipe recipes

Ville Skyttä
2020-06-15 22:32:35 +03:00
parent d62c667ab4
commit d55ed71e50

@@ -11,14 +11,14 @@ array=( $(mycommand) )
If it outputs multiple lines, each of which should be an element:
```sh
# For bash 4.x
# For bash 4.x, must not be in posix mode, may use temporary files
mapfile -t array < <(mycommand)
# For bash 3.x+
# For bash 3.x+, must not be in posix mode, may use temporary files
array=()
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=()
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:
```sh
# For bash
# For bash, uses temporary files
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
IFS=" " read -r -A array <<< "$(mycommand)"
```