Skip to content

Instantly share code, notes, and snippets.

@couchtim
Created September 30, 2012 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save couchtim/3805844 to your computer and use it in GitHub Desktop.
Save couchtim/3805844 to your computer and use it in GitHub Desktop.
Demo of bash/dash subshell when pipe to loop construct
[root@localhost couchbase-stuff]# cat tmp.sh
#! /bin/sh
count=0
/bin/ls /etc | while read f; do
count=$((count + 1));
echo $count $f;
done
# This prints 0 in bash and dash; prints count (e.g., 262) in ksh
echo "at the end, count is: $count"
[root@localhost couchbase-stuff]# sh tmp.sh|tail
254 X11
255 xdg
256 xinetd.conf
257 xinetd.d
258 xml
259 yp.conf
260 yum
261 yum.conf
262 yum.repos.d
at the end, count is: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment