linux-当bash称为sh时,为什么bash的行为有所不同?
作者:互联网
我有一台ubuntu机器,其默认shell设置为bash,并且两种方式都设置为$PATH中的二进制文件:
$which bash
/bin/bash
$which sh
/bin/sh
$ll /bin/sh
lrwxrwxrwx 1 root root 4 Mar 6 2013 /bin/sh -> bash*
但是,当我尝试调用使用the inline file descriptor的脚本(只有bash可以处理,但sh不能处理)时,两个调用的行为不同:
$. ./inline-pipe
reached
$bash ./inline-pipe
reached
$sh ./inline-pipe
./inline-pipe: line 6: syntax error near unexpected token `<'
./inline-pipe: line 6: `done < <(echo "reached")'
我指的示例脚本看起来像这样
#!/bin/sh
while read line; do
if [[ "$line" == "reached" ]]; then echo "reached"; fi
done < <(echo "reached")
真正的更长一点:
#!/bin/sh
declare -A elements
while read line
do
for ele in $(echo $line | grep -o "[a-z]*:[^ ]*")
do
id=$(echo $ele | cut -d ":" -f 1)
elements["$id"]=$(echo $ele | cut -d ":" -f 2)
done
done < <(adb devices -l)
echo ${elements[*]}
解决方法:
当bash作为sh调用时,它(通常)将自身限制为POSIX标准中的功能.进程替换不是那些功能之一,因此是错误.
标签:bash,linux,shell,file-descriptor 来源: https://codeday.me/bug/20191010/1885305.html