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
$ 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?
sh I used is linked to dash, which is in the process of being made POSIX compliant. Using the more venerable
bash solved this.