Why does this "while read" work in a terminal, but not in a shell script?

I ran into this interesting issue while populating my WM bar with info text, which is applied by setting the root window’s title, i.e. xsetroot -name "clever words"

To this end, printing a fortune works fine in a terminal:

fortune -s | while read -r; do xsetroot -name "$REPLY"; done

Yet that same fails when run from a shell script:

#!/bin/sh
cat /tmp/afile | while read; do echo "$REPLY"; done

Produces:

$ sh afilereader
afilereader: 2: read: arg count

Of course this is remedied by assigning our fortune result to a variable, then using xsetroot with said variable. But I’d still like to understand why this does not work in a script.

I realize each command on either side of the pipeline is run within it’s own subshell, but fail to see how their localized variables could affect the while read loop. Or are variables out of scope even between the loop iterations?

What am I missing?

Update: The sh I used is linked to dash, which is in the process of being made POSIX compliant. Using the more venerable bash solved this.

Asked By: invert

||

You appear to be running the first example in bash, and the second in whatever is pointed to by /bin/sh, which is a POSIX shell requiring an argument to be passed specifying the variable you wish to put the input into. Changing the shebang to #!/bin/bash should fix this.

Answered By: Chris Down

In sh syntax, you need

IFS= read -r REPLY

Some shells like ksh, bash and zsh allow read to be called without a variable name but the behavior differs between them. See for instance the output of

printf 'te st\na ' | "$shell" -c 'read; printf "%sn" "<$REPLY>"'

differing on all of bash, zsh, pdksh and ksh93

Answered By: Stéphane Chazelas
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.