Bash – Why does this “while read” work in a terminal, but not in a shell script

bashshell-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.

Best Answer

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.