0

In Bash, the following command

echo foo | while read line; do echo $line; done

outputs foo; however, the following

alias bar="echo foo | while read line; do echo $line; done"
bar

outputs a \n (or empty space). What is causing this difference in behavior?

Zach
  • 115
  • 3
  • 1
    Use a shell function instead, then you won't have issues with quoting. Also see https://unix.stackexchange.com/questions/209123 (about using read correctly) and https://unix.stackexchange.com/questions/68694 (about quoting correctly). – Kusalananda Dec 30 '20 at 17:55

1 Answers1

2

Use single quotes to defer variable expansion:

alias bar='echo foo | while read line; do echo $line; done'