当bash称为sh时,为什么bash的行为有所不同? [英] Why does bash behave differently, when it is called as sh?

查看:132
本文介绍了当bash称为sh时,为什么bash的行为有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台ubuntu机器,其默认shell设置为bash,并且双向都设置为$ PATH中的二进制文件:

I have an ubuntu machine with default shell set to bash and both ways to the binary in $PATH:

$ which bash
/bin/bash
$ which sh
/bin/sh
$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Mar  6  2013 /bin/sh -> bash*

但是当我尝试调用使用内联文件描述符的脚本时(只有bash可以处理,但不能处理) sh)这两个调用的行为不同:

But when I try to call a script that uses the inline file descriptor (that only bash can handle, but not sh) both calls behave differently:

$ . ./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标准中的功能.进程替换不是那些功能之一,因此是错误.

When bash is invoked as sh, it (mostly) restricts itself to features found in the POSIX standard. Process substitution is not one of those features, hence the error.

这篇关于当bash称为sh时,为什么bash的行为有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆