Accessing the output of a Bash pipe with 'read'

Karthik

I'm trying to pipe some data from a Bash pipe into a Bash variable using the read command, like this:

$ echo "Alexander the Grape" | read quot
$ echo $quot
$ 

But quot is empty. Some Googling revealed that this is not a bug; it's an intended feature of Bash. (Section E5 in the FAQ.)

But when I tried the same thing in zsh, it worked. (Ditto for ksh.) Is there any way to make this work in Bash? I really don't want to have to type:

$ quot=$(echo "Alexander the Grape")

Especially for long commands.

Dennis Williamson

For additional reading on this subject, see BashFAQ/024.

There are a bunch of ways to do variable assignments in Bash:

var=$(command)
read var <<< $(command)
read var <<< EOF
$(command)
EOF
printf -v var "%s" $(command)

etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash: Pipe output into a table

From Dev

Bash: Pipe output into a table

From Dev

Pipe every bash output to file

From Dev

Bash: Pipe output into background process?

From Dev

Bash: pipe 'find' output into 'readarray'

From Dev

Bash: pipe 'find' output into 'readarray'

From Dev

Read stdin in chunks in Bash pipe

From Dev

Bash inline pipe with read behavior

From Dev

Read console output without redirect or pipe

From Java

Pipe output and capture exit status in Bash

From Dev

bash exec sending output to a pipe, how?

From Dev

Bash function to robustly pipe terminal output to vim

From Dev

How to invoke vim editor and pipe output to bash

From Dev

I want to pipe an output to multiple files in bash

From Dev

how to pipe output of traced bash debugging?

From Dev

Bash - pipe multiple grep and print output

From Dev

Pipe Bash command output to stdout and to a variable

From Dev

How to pipe data to interactive bash script and pipe output to another command?

From Dev

In bash, read after a pipe is not setting values

From Dev

bash read netcat status in pipe then exit

From Dev

Bash while read : output issue

From Dev

How pipe std and err output to separate commands in bash script?

From Dev

Redirect bash pipe output as python script second argument

From Dev

Pipe output from command in Git Bash to Powershell script on windows

From Dev

How pipe std and err output to separate commands in bash script?

From Dev

Bash: How to direct output to both stderr and to stdout, to pipe into another command?

From Dev

How to pipe all bash terminal output through command

From Dev

Using read -p in a bash script that was executed from pipe

From Dev

Using read -p in a bash script that was executed from pipe

Related Related

  1. 1

    Bash: Pipe output into a table

  2. 2

    Bash: Pipe output into a table

  3. 3

    Pipe every bash output to file

  4. 4

    Bash: Pipe output into background process?

  5. 5

    Bash: pipe 'find' output into 'readarray'

  6. 6

    Bash: pipe 'find' output into 'readarray'

  7. 7

    Read stdin in chunks in Bash pipe

  8. 8

    Bash inline pipe with read behavior

  9. 9

    Read console output without redirect or pipe

  10. 10

    Pipe output and capture exit status in Bash

  11. 11

    bash exec sending output to a pipe, how?

  12. 12

    Bash function to robustly pipe terminal output to vim

  13. 13

    How to invoke vim editor and pipe output to bash

  14. 14

    I want to pipe an output to multiple files in bash

  15. 15

    how to pipe output of traced bash debugging?

  16. 16

    Bash - pipe multiple grep and print output

  17. 17

    Pipe Bash command output to stdout and to a variable

  18. 18

    How to pipe data to interactive bash script and pipe output to another command?

  19. 19

    In bash, read after a pipe is not setting values

  20. 20

    bash read netcat status in pipe then exit

  21. 21

    Bash while read : output issue

  22. 22

    How pipe std and err output to separate commands in bash script?

  23. 23

    Redirect bash pipe output as python script second argument

  24. 24

    Pipe output from command in Git Bash to Powershell script on windows

  25. 25

    How pipe std and err output to separate commands in bash script?

  26. 26

    Bash: How to direct output to both stderr and to stdout, to pipe into another command?

  27. 27

    How to pipe all bash terminal output through command

  28. 28

    Using read -p in a bash script that was executed from pipe

  29. 29

    Using read -p in a bash script that was executed from pipe

HotTag

Archive