lemoncube.com Search: (normal)
Articles
Links
News
Projects

Disclaimer

Getting around bash oddities (for ksh folk)

BASH has some major oddities for long term ksh users. If like me you do things like:

   t=0
   cat somefile |while read a b c 
   do
      t=$((t+a+b+c))
   done
   echo "total thing is $t"
And also:
   echo $((12+34)) | read var
then you'll get quite pissed off with bash because none of these things work. The technical explanation is that the pipe symbol always creates a subprocess. Because you perform your read in a subprocess the variable only gets set within the subprocess, hence when the subprocess ends the contents of the variable also disappear.

Here's how to do that loop properly in bash:

   t=0
   while read a b c 
   do
      t=$((t+a+b+c))
   done <somefile
   echo "total thing is $t"
And for the simple variable setting, there are a few options:
   # this is specific to bash (note the spaces)
   read var < <((echo $((12+34)) ))

   # or this one
   read var <<EOF
$(echo $((12+34)) )
EOF

pablo , 2003-12-06 21:23:13