220

I am trying to get bash to process data from stdin that gets piped into, but no luck. What I mean is none of the following work:

echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=

echo "hello world" | read test; echo test=$test
test=

echo "hello world" | test=`cat`; echo test=$test
test=

where I want the output to be test=hello world. I've tried putting "" quotes around "$test" that doesn't work either.

codeforester
  • 28,846
  • 11
  • 78
  • 104
ldog
  • 10,559
  • 9
  • 52
  • 68
  • 1
    Your example.. echo "hello world" | read test; echo test=$test worked fine for me.. result: test=hello world ; what environment are running this under? I'm using bash 4.2.. – alex.pilon Jul 21 '12 at 14:43
  • Do you want multiple lines in a single read? Your example only shows one line, but the problem description is unclear. – Charles Duffy Oct 04 '12 at 14:08
  • 3
    @alex.pilon, I'm running Bash version 4.2.25, and his example does not work for me too. May be that's a matter of a Bash runtime option or environment variable? I've the example does not work with Sh neither, so may be Bash can try to be compatible with Sh? – Hibou57 Jul 15 '14 at 23:45
  • 2
    @Hibou57 - I tried this again in bash 4.3.25 and it no longer works. My memory is fuzzy on this and I'm not sure what I may have done to get it to work. – alex.pilon Oct 24 '14 at 18:20
  • 2
    @Hibou57 @alex.pilon the last cmd in a pipe should affect the vars in bash4>=4.2 with `shopt -s lastpipe` -- http://tldp.org/LDP/abs/html/bashver4.html#LASTPIPEOPT – imz -- Ivan Zakharyaschev May 27 '16 at 12:49
  • @alex.pilon the $test variable was probably already bound from a previous attempt. Repeating this with a fresh session will fail as expected. – Tasos Papastylianou May 06 '20 at 10:31
  • We can use _mkfifo(1)_ to create a named pipe, say `/tmp/p` (mind the access rights). Redirect the data source to it, and leave the source to the background `echo "..." > /tmp/p &`. We would be blocked here waiting for consumption otherwise. Then redirect the input of `read` from the pipe, and stay with it (do not end with `&`). We stay because we are waiting for data, and because, with `&`, the receiving variable would be in a subprocess, invisible to the main one. This named pipe method works both interactively and in script, allows updates on the go, and is quite portable. – Yuning May 25 '20 at 00:12

16 Answers16

178

Use

IFS= read var << EOF
$(foo)
EOF

You can trick read into accepting from a pipe like this:

echo "hello world" | { read test; echo test=$test; }

or even write a function like this:

read_from_pipe() { read "$@" <&0; }

But there's no point - your variable assignments may not last! A pipeline may spawn a subshell, where the environment is inherited by value, not by reference. This is why read doesn't bother with input from a pipe - it's undefined.

FYI, http://www.etalabs.net/sh_tricks.html is a nifty collection of the cruft necessary to fight the oddities and incompatibilities of bourne shells, sh.

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
yardena
  • 2,170
  • 1
  • 16
  • 6
  • You can make the assignment last by doing this instead: `test=``echo "hello world" | { read test; echo $test; }``` – Compholio Oct 31 '12 at 15:22
  • 2
    Let's try this again (apparently escaping backticks in this markup is fun): `test=\`echo "hello world" | { read test; echo $test; }\`` – Compholio Oct 31 '12 at 15:29
  • can I ask why you used `{}` instead of `()` in grouping those two commands? – Jürgen Paul Aug 10 '13 at 13:42
  • 4
    The trick is not in making `read` take input from the pipe, but in using the variable in the same shell that executes the `read`. – chepner Oct 02 '13 at 17:02
  • the extra `{ }` block trick won't be needed in bash4>=4.2 with `shopt -s lastpipe` -- http://tldp.org/LDP/abs/html/bashver4.html#LASTPIPEOPT – imz -- Ivan Zakharyaschev May 27 '16 at 12:50
  • That works perfectly http://stackoverflow.com/questions/6980090/how-to-read-from-file-or-stdin-in-bash – Buzut Jun 12 '16 at 14:40
  • 1
    Got `bash permission denied` when trying to use this solution. My case is quite different, but I couldn't find an answer for it anywhere, what worked for me was (a different example but similar usage): `pip install -U echo $(ls -t *.py | head -1)`. In case, someone ever has a similar problem and stumbles upon this answer like me. – ivan_bilan Mar 14 '19 at 15:25
116

if you want to read in lots of data and work on each line separately you could use something like this:

cat myFile | while read x ; do echo $x ; done

if you want to split the lines up into multiple words you can use multiple variables in place of x like this:

cat myFile | while read x y ; do echo $y $x ; done

alternatively:

while read x y ; do echo $y $x ; done < myFile

But as soon as you start to want to do anything really clever with this sort of thing you're better going for some scripting language like perl where you could try something like this:

perl -ane 'print "$F[0]\n"' < myFile

There's a fairly steep learning curve with perl (or I guess any of these languages) but you'll find it a lot easier in the long run if you want to do anything but the simplest of scripts. I'd recommend the Perl Cookbook and, of course, The Perl Programming Language by Larry Wall et al.

Nick
  • 1,822
  • 1
  • 12
  • 9
53

This is another option

$ read test < <(echo hello world)

$ echo $test
hello world
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
45

read won't read from a pipe (or possibly the result is lost because the pipe creates a subshell). You can, however, use a here string in Bash:

$ read a b c <<< $(echo 1 2 3)
$ echo $a $b $c
1 2 3

But see @chepner's answer for information about lastpipe.

Dennis Williamson
  • 303,596
  • 86
  • 357
  • 418
41

I'm no expert in Bash, but I wonder why this hasn't been proposed:

stdin=$(cat)

echo "$stdin"

One-liner proof that it works for me:

$ fortune | eval 'stdin=$(cat); echo "$stdin"'
djanowski
  • 4,951
  • 1
  • 23
  • 15
  • 4
    That's probably because "read" is a bash command, and cat is a separate binary that will be launched in a subprocess, so it's less efficient. – dj_segfault Feb 08 '13 at 14:25
  • 13
    sometimes simplicity and clarity trump efficiency :) – Rondo Dec 18 '14 at 04:05
  • 7
    definitely the most straight-forward answer – drwatsoncode Mar 06 '15 at 16:38
  • The problem I ran in to with this is that if a pipe isn't used, the script hangs. – Dale Anderson Oct 26 '15 at 19:43
  • @DaleA Well, of course. Any program that tries to read from standard input will "hang" if nothing is fed to it. – djanowski Oct 27 '15 at 12:21
  • 1
    @djanowski but it's not necessarily the expected behaviour of a given script. If only there was a way to handle the absence of stdin gracefully, and fall back to 'regular' behaviour if it's not present. [This](http://superuser.com/a/747905/34981) post almost has it - it accepts either arguments or stdin. The only thing missing is being able to provide a usage helper if neither are present. – Dale Anderson Oct 27 '15 at 16:33
  • Thanks, this answer in my opinion is the best. Fixed my issue. – Flare Cat Jan 19 '16 at 02:00
  • Simple and elegant solution. –  Feb 13 '16 at 03:02
  • 1
    While searching for alternatives to the accepted answer, I decided to go with something similar to this answer for my use case :) ```${@:-$(cat)}``` – tinnick May 09 '20 at 12:36
27

bash 4.2 introduces the lastpipe option, which allows your code to work as written, by executing the last command in a pipeline in the current shell, rather than a subshell.

shopt -s lastpipe
echo "hello world" | read test; echo test=$test
chepner
  • 389,128
  • 51
  • 403
  • 529
  • 2
    ah! so good, this. if testing in an interactive shell, also: "set +m" (not required in an .sh script) – XXL May 22 '16 at 15:54
15

The syntax for an implicit pipe from a shell command into a bash variable is

var=$(command)

or

var=`command`

In your examples, you are piping data to an assignment statement, which does not expect any input.

mob
  • 110,546
  • 17
  • 138
  • 265
  • 4
    Because $() can be nested easily. Think in JAVA_DIR=$(dirname $(readlink -f $(which java))), and try it with `. You will need to escape three times! – albfan Oct 20 '12 at 20:17
15

A smart script that can both read data from PIPE and command line arguments:

#!/bin/bash
if [[ -p /dev/stdin ]]
    then
    PIPE=$(cat -)
    echo "PIPE=$PIPE"
fi
echo "ARGS=$@"

Output:

$ bash test arg1 arg2
ARGS=arg1 arg2

$ echo pipe_data1 | bash test arg1 arg2
PIPE=pipe_data1
ARGS=arg1 arg2

Explanation: When a script receives any data via pipe, then the /dev/stdin (or /proc/self/fd/0) will be a symlink to a pipe.

/proc/self/fd/0 -> pipe:[155938]

If not, it will point to the current terminal:

/proc/self/fd/0 -> /dev/pts/5

The bash [[ -p option can check it it is a pipe or not.

cat - reads the from stdin.

If we use cat - when there is no stdin, it will wait forever, that is why we put it inside the if condition.

Seff
  • 1,048
  • 14
  • 14
10

The first attempt was pretty close. This variation should work:

echo "hello world" | { test=$(< /dev/stdin); echo "test=$test"; };

and the output is:

test=hello world

You need braces after the pipe to enclose the assignment to test and the echo.

Without the braces, the assignment to test (after the pipe) is in one shell, and the echo "test=$test" is in a separate shell which doesn't know about that assignment. That's why you were getting "test=" in the output instead of "test=hello world".

jbuhacoff
  • 901
  • 1
  • 11
  • 13
9

In my eyes the best way to read from stdin in bash is the following one, which also lets you work on the lines before the input ends:

while read LINE; do
    echo $LINE
done < /dev/stdin
Jaleks
  • 330
  • 2
  • 13
7

Because I fall for it, I would like to drop a note. I found this thread, because I have to rewrite an old sh script to be POSIX compatible. This basically means to circumvent the pipe/subshell problem introduced by POSIX by rewriting code like this:

some_command | read a b c

into:

read a b c << EOF
$(some_command)
EOF

And code like this:

some_command |
while read a b c; do
    # something
done

into:

while read a b c; do
    # something
done << EOF
$(some_command)
EOF

But the latter does not behave the same on empty input. With the old notation the while loop is not entered on empty input, but in POSIX notation it is! I think it's due to the newline before EOF, which cannot be ommitted. The POSIX code which behaves more like the old notation looks like this:

while read a b c; do
    case $a in ("") break; esac
    # something
done << EOF
$(some_command)
EOF

In most cases this should be good enough. But unfortunately this still behaves not exactly like the old notation if some_command prints an empty line. In the old notation the while body is executed and in POSIX notation we break in front of the body.

An approach to fix this might look like this:

while read a b c; do
    case $a in ("something_guaranteed_not_to_be_printed_by_some_command") break; esac
    # something
done << EOF
$(some_command)
echo "something_guaranteed_not_to_be_printed_by_some_command"
EOF
hd-quadrat
  • 71
  • 1
  • 4
  • `[ -n "$a" ] || break` should also work – but the problem about missing actual empty lines remains – joki May 19 '21 at 13:30
3

Piping something into an expression involving an assignment doesn't behave like that.

Instead, try:

test=$(echo "hello world"); echo test=$test
bta
  • 39,855
  • 5
  • 67
  • 92
3

The following code:

echo "hello world" | ( test=($(< /dev/stdin)); echo test=$test )

will work too, but it will open another new sub-shell after the pipe, where

echo "hello world" | { test=($(< /dev/stdin)); echo test=$test; }

won't.


I had to disable job control to make use of chepnars' method (I was running this command from terminal):

set +m;shopt -s lastpipe
echo "hello world" | read test; echo test=$test
echo "hello world" | test="$(</dev/stdin)"; echo test=$test

Bash Manual says:

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: job control is turned off by default in a non-interactive shell and thus you don't need the set +m inside a script.

Community
  • 1
  • 1
Jahid
  • 18,228
  • 8
  • 79
  • 95
1

I think you were trying to write a shell script which could take input from stdin. but while you are trying it to do it inline, you got lost trying to create that test= variable. I think it does not make much sense to do it inline, and that's why it does not work the way you expect.

I was trying to reduce

$( ... | head -n $X | tail -n 1 )

to get a specific line from various input. so I could type...

cat program_file.c | line 34

so I need a small shell program able to read from stdin. like you do.

22:14 ~ $ cat ~/bin/line 
#!/bin/sh

if [ $# -ne 1 ]; then echo enter a line number to display; exit; fi
cat | head -n $1 | tail -n 1
22:16 ~ $ 

there you go.

Mathieu J.
  • 1,408
  • 14
  • 22
0

I wanted something similar - a function that parses a string that can be passed as a parameter or piped.

I came up with a solution as below (works as #!/bin/sh and as #!/bin/bash)

#!/bin/sh

set -eu

my_func() {
    local content=""
    
    # if the first param is an empty string or is not set
    if [ -z ${1+x} ]; then
 
        # read content from a pipe if passed or from a user input if not passed
        while read line; do content="${content}$line"; done < /dev/stdin

    # first param was set (it may be an empty string)
    else
        content="$1"
    fi

    echo "Content: '$content'"; 
}


printf "0. $(my_func "")\n"
printf "1. $(my_func "one")\n"
printf "2. $(echo "two" | my_func)\n"
printf "3. $(my_func)\n"
printf "End\n"

Outputs:

0. Content: ''
1. Content: 'one'
2. Content: 'two'
typed text
3. Content: 'typed text'
End

For the last case (3.) you need to type, hit enter and CTRL+D to end the input.

Jimmix
  • 3,933
  • 2
  • 19
  • 39
-1

How about this:

echo "hello world" | echo test=$(cat)
bingles
  • 9,662
  • 6
  • 65
  • 76