0
I am trying to store output from a pipe into a variable.
After reading this post (Accessing the output of a Bash pipe with 'read'), I had some doubts remembering if I was ever successful in doing that, on a Unix system.
Then I found this post (How to loop through file names returned by find?), and was reminded that it works, sometimes using read -r <varname>
too.
Environment:
User@Computer /cygdrive/...
$ echo $SHELL
/bin/bash
User@Computer /cygdrive/...
$ echo $BASH_VERSION
4.4.5(1)-release
User@Computer /cygdrive/...
$ uname -a
CYGWIN_NT-10.0 Computer 2.6.1(0.305/5/3) 2016-12-16 11:55 x86_64
User@Computer /cygdrive/...
$ read --help
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.
Attempts:
User@Computer /cygdrive/...
$ echo $var
User@Computer /cygdrive/...
$ echo "hi" | read var
User@Computer /cygdrive/...
$ echo $var
User@Computer /cygdrive/...
$ echo "hi" | read -r var
User@Computer /cygdrive/...
$ echo $var
User@Computer /cygdrive/...
$ read var
hi
User@Computer /cygdrive/...
$ echo $var
hi
User@Computer /cygdrive/...
$ echo "hi" | cat
hi
User@Computer /cygdrive/...
$ echo -e '1\n2\n3\n' | xargs
1 2 3
I also tried this in bash.exe
's own process and window:
User@Computer /cygdrive/...
$ echo "hi" | read var;echo $var;
Also doing the same in the "Bash on Ubuntu on Windows" bash.exe
:
echo "hi" | read var;echo $var;
Still no successful result of the assignment.
I am aware I can work around this method, instead probably using $()
or the redirection arrows.
Is there a way to do this using this read
line command method on Windows?
Update
To some of the answers, I knew some would post alternatives like $()
. This is why I intentionally mentioned this method already, including a link where several were already listed, along with specifically asking if there was a way to use the read
line utility to store input into a variable, not using redirection, but instead piping data through a command chain, as I had done before on other systems, but this time on Windows.
1
this happen because read is executed in a subshell, and any variable created in that subshell is destroyed upon exit. see: http://mywiki.wooledge.org/BashFAQ/024 . the simplest workaround is to use named pipe
– Sharuzzaman Ahmat Raslan – 2017-08-02T03:05:00.3931Your second link works only in the form
something | while read var; do use $var; done
because the entire while loop forms the right-end of the pipe and runs in a subshell; the setting of var is gone after the end of the while loop. @SharuzzamanAhmatRaslan: although bash 4.2+ (as in this Q) has shopt lastpipe to change this in a script. – dave_thompson_085 – 2017-08-02T05:52:58.900Interesting: https://stackoverflow.com/questions/36340599/how-does-shopt-s-lastpipe-affect-bash-script-behavior#36340724
Probably won't use this way, as I still see several different versions of
bash
around, and I want to be more compatible with them.