无法在BASH中获取WHILE变量的值

时间:2012-12-19 07:17:05

标签: bash

我有一个代码:

od -An -t x1 <"$file" |       
while read line;do               
  for char in $line;do  
    if [ $needspace -eq 0 ]
    then 
      export  hexs=$hexs`printf "%.2s" "$char"` 
      needspace=1
    else
      export hexs=$hexs`printf "%.2s " "$char"`    
      needspace=0  
    fi
    if [ `printf "%d" "0x$char" ` -lt 32 ] || [ `printf "%d" "0x$char"` -gt 126 ]
    then
      text=$text.
    else
      text=$text`echo -e "\x$char"`  
    fi
    charsnow=$(($charsnow+1))
  done  
  sup=1   
  echo $sup
done
echo $sup

问题是:首先echo $sup写1 但第二个echo $sup没有写任何内容。为什么我不能从“while”中获得$ sup?

1 个答案:

答案 0 :(得分:3)

你有一个子壳,所以不会发生副作用。 http://mywiki.wooledge.org/BashFAQ/024

相关问题