Sample Header Ad - 728x90

Bash: Assign output of pipe to a variable

130 votes
8 answers
303488 views
I am trying to get the output of a pipe into a variable. I tried the following things: echo foo | myvar=$($myvar is empty. I don’t want to do: myvar=$(echo foo) Because I don’t want to spawn a subshell. Any ideas? **Edit:** I don’t want to spawn a subshell because the command before the pipe needs to edit global variables, which it can’t do in a subshell. Can it? The echo thing is just for simplification. It’s more like: complex_function | myvar=$(
Asked by Parckwart (1301 rep)
Jan 17, 2017, 10:13 AM
Last activity: Aug 23, 2024, 08:45 AM