Skip to main content
added 398 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

When using a pipeline, bash runs the commands in subshellssubshells¹. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it. You also likely want the -t option so as not to store that line delimiters in the array members as they are not part of the file names.

Use process substitution:

readarray -t FILES < <(find .) 

Note that it doesn't work for files with newlines in their namespaths. If that couldUnless you can guarantee if won't be the case, you need a more elaborate syntaxyou'd want to use NUL delimited records instead of newline delimited ones:

readarray -dtd '' < <(find . -print0) 

(the -d option was added in bash 4.4)


¹ except for the last pipe component when using the lastpipe option, but that's only for non-interactive invocations of bash.

When using a pipeline, bash runs the commands in subshells. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it.

Use process substitution:

readarray FILES < <(find) 

Note that it doesn't work for files with newlines in their names. If that could be the case, you need a more elaborate syntax:

readarray -d '' < <(find -print0) 

When using a pipeline, bash runs the commands in subshells¹. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it. You also likely want the -t option so as not to store that line delimiters in the array members as they are not part of the file names.

Use process substitution:

readarray -t FILES < <(find .) 

Note that it doesn't work for files with newlines in their paths. Unless you can guarantee if won't be the case, you'd want to use NUL delimited records instead of newline delimited ones:

readarray -td '' < <(find . -print0) 

(the -d option was added in bash 4.4)


¹ except for the last pipe component when using the lastpipe option, but that's only for non-interactive invocations of bash.

added 102 characters in body
Source Link
choroba
  • 49.6k
  • 7
  • 92
  • 119

When using a pipeline, bash runs the commands in subshells. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it.

Use process substitution:

readarray FILES < <(find) 

Note that it doesn't work for files with newlines in their names. If that could be the case, you need a more elaborate syntax:

readarray -d '' < <(find -print0) 

When using a pipeline, bash runs the commands in subshells. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it.

Use process substitution:

readarray FILES < <(find) 

Note that it doesn't work for files with newlines in their names.

When using a pipeline, bash runs the commands in subshells. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it.

Use process substitution:

readarray FILES < <(find) 

Note that it doesn't work for files with newlines in their names. If that could be the case, you need a more elaborate syntax:

readarray -d '' < <(find -print0) 
Source Link
choroba
  • 49.6k
  • 7
  • 92
  • 119

When using a pipeline, bash runs the commands in subshells. Therefore, the array is populated, but in a subshell, so the parent shell has no access to it.

Use process substitution:

readarray FILES < <(find) 

Note that it doesn't work for files with newlines in their names.