Skip to main content
added 187 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26

You could just use the read command and Command Grouping with curley braces: echo foo | { read myvar; echo $myvar; } except you want to use "global variables", which I think you mean "shell variables" (declare -p to list, or set | grep myvar or set -o posix; set). https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

Using Command Grouping executes them in the "current shell context", however that is still a subshell as soon as a pipe is used, even if the braces surround the entire line { echo foo | read myvar; echo $myvar; }.

So you have to use shell options. (it seems, because the command line is interactive by default):

shopt -s lastpipe      # sets shell option for lastpipe on
set +m                 # turns job control off, part of interactive shell
echo foo | read myvar; echo $myvar
# foo

With those shell options, chaining works read works echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2, to produce foo bar.

PS, you can put those shell options in your .bashrc, but I'm not sure if there are downsides to that beside possible incompatibility with scripts that use interactive shell flag -i.

You could just use the read command and Command Grouping with curley braces: echo foo | { read myvar; echo $myvar; } except you want to use "global variables", which I think you mean "shell variables" (declare -p to list, or set | grep myvar or set -o posix; set). https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

Using Command Grouping executes them in the "current shell context", however that is still a subshell as soon as a pipe is used, even if the braces surround the entire line { echo foo | read myvar; echo $myvar; }.

So you have to use shell options. (it seems, because the command line is interactive by default):

shopt -s lastpipe      # sets shell option for lastpipe on
set +m                 # turns job control off, part of interactive shell
echo foo | read myvar; echo $myvar
# foo

With those shell options, chaining works read works echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2, to produce foo bar.

You could just use the read command and Command Grouping with curley braces: echo foo | { read myvar; echo $myvar; } except you want to use "global variables", which I think you mean "shell variables" (declare -p to list, or set | grep myvar or set -o posix; set). https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

Using Command Grouping executes them in the "current shell context", however that is still a subshell as soon as a pipe is used, even if the braces surround the entire line { echo foo | read myvar; echo $myvar; }.

So you have to use shell options. (it seems, because the command line is interactive by default):

shopt -s lastpipe      # sets shell option for lastpipe on
set +m                 # turns job control off, part of interactive shell
echo foo | read myvar; echo $myvar
# foo

With those shell options, chaining works read works echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2, to produce foo bar.

PS, you can put those shell options in your .bashrc, but I'm not sure if there are downsides to that beside possible incompatibility with scripts that use interactive shell flag -i.

added 234 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26

ForYou could just use the read command and Command Grouping with curley braces: echo foo | { read myvar; echo $myvar; } except you want to use "global variables", which I think you mean "shell variables" (declare -p to list, or set | grep myvar or set -o posix; set). https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

Using Command Grouping executes them in the "current shell context", however that is still a subshell as soon as a pipe is used, even if the braces surround the entire line { echo foo | read myvar; echo $myvar; }.

So you have to use shell options. (whichit seems, because the command line is interactive by default now), use:

shopt -s lastpipe      # sets shell option for lastpipe on
set +m                 # turns job control off, part of interactive shell
echo foo | read myvar; echo $myvar
# foo

SomehowWith those shell options, chaining works read works echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2 works, to produce foo bar, even though there are more than one pipes. Maybe lastpipe is relative.

However Kusalananda's answer can also be used chained and doesnt require shell options, so should work for cli and scripts. echo 'hello' | { read message; echo "$message"; } | { read var1; echo $var1" world"; } -> hello world.

BTW, using curly braces is called Command Grouping in Unix: https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

For the command line (which is interactive by default now), use:

shopt -s lastpipe      # sets shell option for lastpipe
set +m                 # turns job control off
echo foo | read myvar; echo $myvar

Somehow echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2 works to produce foo bar, even though there are more than one pipes. Maybe lastpipe is relative.

However Kusalananda's answer can also be used chained and doesnt require shell options, so should work for cli and scripts. echo 'hello' | { read message; echo "$message"; } | { read var1; echo $var1" world"; } -> hello world.

BTW, using curly braces is called Command Grouping in Unix: https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

You could just use the read command and Command Grouping with curley braces: echo foo | { read myvar; echo $myvar; } except you want to use "global variables", which I think you mean "shell variables" (declare -p to list, or set | grep myvar or set -o posix; set). https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

Using Command Grouping executes them in the "current shell context", however that is still a subshell as soon as a pipe is used, even if the braces surround the entire line { echo foo | read myvar; echo $myvar; }.

So you have to use shell options. (it seems, because the command line is interactive by default):

shopt -s lastpipe      # sets shell option for lastpipe on
set +m                 # turns job control off, part of interactive shell
echo foo | read myvar; echo $myvar
# foo

With those shell options, chaining works read works echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2, to produce foo bar.

added 136 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26

For the command line (which is interactive by default now), use:

shopt -s lastpipe      # sets shell option for lastpipe
set +m                 # turns job control off
echo foo | read myvar; echo $myvar

Somehow echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2 works to produce foo bar, even though there are more than one pipes. Maybe lastpipe is relative.

However Kusalananda's answer can also be used chained and doesnt require shell options, so should work for cli and scripts. echo 'hello' | { read message; echo "$message"; } | { read var1; echo $var1" world"; } -> hello world.

BTW, using curly braces is called Command Grouping in Unix: https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

For the command line (which is interactive by default now), use:

shopt -s lastpipe      # sets shell option for lastpipe
set +m                 # turns job control off
echo foo | read myvar; echo $myvar

Somehow echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2 works to produce foo bar, even though there are more than one pipes. Maybe lastpipe is relative.

However Kusalananda's answer can also be used chained and doesnt require shell options, so should work for cli and scripts. echo 'hello' | { read message; echo "$message"; } | { read var1; echo $var1" world"; } -> hello world.

For the command line (which is interactive by default now), use:

shopt -s lastpipe      # sets shell option for lastpipe
set +m                 # turns job control off
echo foo | read myvar; echo $myvar

Somehow echo foo | read myvar; echo $myvar" bar" | read myvar2; echo $myvar2 works to produce foo bar, even though there are more than one pipes. Maybe lastpipe is relative.

However Kusalananda's answer can also be used chained and doesnt require shell options, so should work for cli and scripts. echo 'hello' | { read message; echo "$message"; } | { read var1; echo $var1" world"; } -> hello world.

BTW, using curly braces is called Command Grouping in Unix: https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html

added 3 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26
Loading
added 229 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26
Loading
added 50 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26
Loading
added 50 characters in body
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26
Loading
Source Link
alchemy
  • 797
  • 1
  • 11
  • 26
Loading