I am trying to store stdin
into a variable in a pipeline:
$ echo 'message' | variable="-" ; echo $variable
I know I can do a script for this; but I am just trying this way to understand.
Any idea why this isn't working?
I am trying to store stdin
into a variable in a pipeline:
$ echo 'message' | variable="-" ; echo $variable
I know I can do a script for this; but I am just trying this way to understand.
Any idea why this isn't working?
You can do this:
echo 'message' | ( var="$(< /dev/stdin)"; echo "$var" )
Or:
echo 'message' | { var="$(< /dev/stdin)"; echo $var; }
Note: ( ... )
opens another new subshell after pipe.
lastpipe
bash>=4.2
):
set +m;shopt -s lastpipe # set +m disables job control
echo "hello world" | read test; echo test=$test
echo "hello world" | test="$(</dev/stdin)"; echo test=$test
lastpipe
If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
Note that, job control is turned off by default in non-interactive shell.