2014-01-22 55 views
2

在噓使用<<<拋出拍的shell錯誤不是在bash

[SunOs] /opt # sh 
[\h] \w \$ read -a array <<< "1 2 3";echo ${array[1]} 
syntax error: `<' unexpected 

在bash

[SunOs] ~ # bash 
[SunOs] ~ # read -a array <<< "1 2 3";echo ${array[1]} 
2 

爲什麼錯誤thorwn在 「sh」 的殼,我用我的SunOS 5.10 Generic_147440 -10 sun4v sparc sun4v

回答

3

sh中不支持Herestrings。

這會導致錯誤,當您嘗試使用sh運行它時。

+0

謝謝你的位置參數數組[email protected]它不是在SunOS的支持,我可以在Linux和MacOSX上運行相同的命令 – anish

+0

@anish此外'sh'可用的'read'不支持'-a'。 – devnull

+3

@anish:'sh'不是'bash'。 – choroba

1

作爲一種變通方法,你可以使用內置的POSIX命令set到你的論點分配給位置參數$1$2,......或分別

{ 
IFS="`printf ' \n\t'`" 
export IFS 
printf '%s' "$IFS" | od -b 
set -- `printf '%s' "1 2 3"` 
echo "$0" 
echo "$1" 
echo "$2" 
echo "$3" 
echo "[email protected]" 
} 
+0

感謝您的建議 – anish

相關問題