What happens when reading into a variable in a pipeline?
Asked Answered
H

2

13
echo hello | read str
echo $str

This read is executed after the pipeline, which means that the output of the echo gets read into str - but because it is after a pipe, the contents of str are now in a subshell that cannot be read by the parent shell.

What happens in to the contents of str? Does the pipe create a subshell, and then once the content are read into str, does the parent process kill the child process and str is erased - or does the contents of str live on somewhere outside the shell? Like how do we see what is in the subshells? Can we access subshells from the parent shells?

Helenahelene answered 26/10, 2017 at 6:3 Comment(0)
A
11

In your example, $str exists inside a subshell and by default, it disappears once the line has finished. A child process cannot modify its parent.

Aside from changing the shell option lastpipe, you can also change the code to avoid using a pipe. In this case, you could use:

read str < <(your command) 
# or
str=$(your command)

Both of these create subshells too, but $str is assigned to in the parent process.

Ailee answered 26/10, 2017 at 6:36 Comment(0)
S
12

The value of ${str} only exists during the lifetime of the subshell. Whether the left or right side of the pipe will be parent or subshell depends on the specific shell and the shell version.

Bash 4.x has an option shopt -s lastpipe to run the last command of a pipeline in the parent shell, like ksh93 does by default. The value of $str will then persist.

Saboteur answered 26/10, 2017 at 6:10 Comment(0)
A
11

In your example, $str exists inside a subshell and by default, it disappears once the line has finished. A child process cannot modify its parent.

Aside from changing the shell option lastpipe, you can also change the code to avoid using a pipe. In this case, you could use:

read str < <(your command) 
# or
str=$(your command)

Both of these create subshells too, but $str is assigned to in the parent process.

Ailee answered 26/10, 2017 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.