LoginSignup
3
2

More than 5 years have passed since last update.

UbuntuのBシェル

Last updated at Posted at 2017-11-07

UbuntuのBシェルはdashというシェルへのシンボリックリンクとなっていて、Bashの拡張変数展開に失敗するので注意。shebang行を #!/bin/sh とすると Bad substitution が発生する。

dash
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
$ export LANG=C
$ which sh
/bin/sh
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May 11 14:15 /bin/sh -> dash
$ ls -l /bin/dash
-rwxr-xr-x 1 root root 154072 Feb 18  2016 /bin/dash
$ dash
$ echo $SHELL
/bin/bash
$ test='ABCDEFG'
$ echo $test
ABCDEFG
$ echo ${test:2:3}    # Bashの拡張変数展開に失敗; Bashならば CDE と表示
dash: 12: Bad substitution

テキストファイルを読み込んで配列にする場合もエラーが出てしまう。

$ cat sports.sh
#!/bin/sh
sports=(`cat sports.txt`)
for sport in ${sports[@]}
do
  echo $sport
done
exit
$ ./sports.sh
./sports.sh: 2: ./sports.sh: Syntax error: "(" unexpected
$ /bin/bash sports.sh
soccer
baseball
...
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2