今天看到一个shell函数,开始不理解,后来才明白
function in_path(){
cmd=$1;
path=$2;
oldIFS=$IFS
IFS=":"
retval=1
for directory in $path
do
if [ -x $directory/$cmd ];then
return 0
fi
done
IFS=$oldIFS
return $retval
}
in_path cd $PATH
由于$PATH是系统环境变量,以:分隔,当遍历$path时,需要改变默认IFS,让IFS=":",for directory in $path 调用时使用到了系统IFS,
默认IFS为空白字符