0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MacとRaspberry Piでshとbashの違いを確認

Posted at

環境

macOS Catalina 10.15.7
Raspbian 10.3

実行するシェルスクリプト

test_sh.sh
# !/bin/sh
echo {0..10}                                                                                  
test_bash.sh
# !/bin/bash
echo {0..10}                                                                                  

Mac上で実行

$ chmod +x test_sh.sh
$ chmod +x test_bash.sh

実行権限を与える。それぞれ実行すると、

$ ./test_sh.sh
0 1 2 3 4 5 6 7 8 9 10

$ ./test_bash.sh
0 1 2 3 4 5 6 7 8 9 10

shでもbashでも同じ結果が得られる。

/bin/shのシンボリックリンクを調べてみる。

$ ls -l /bin/sh
-rwxr-xr-x  1 root  wheel  31440  9 22  2020 /bin/sh

bashにリンクしているわけでない

$ /bin/sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

/bin/sh --version/bin/bash --versionは同じ内容が返ってくる。

Macではshbashはほぼ同じもの。違いがあるとしたら、

check_option.sh
set -o
$ sh check_option.sh | grep posix
posix          	on

$ bash check_option.sh | grep posix
posix          	off

posixと言うオプションがbashではoffでshだとonになっている点。

Raspberry Pi

$ chmod +x test_sh.sh
$ chmod +x test_bash.sh

実行権限を与える。それぞれ実行すると、

$ ./test_sh.sh
{0..10}

$ ./test_bash.sh
0 1 2 3 4 5 6 7 8 9 10

shbashで得られる結果が異なる。

/bin/shのシンボリックリンクを調べてみる。

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4  2月 14  2020 /bin/sh -> dash

dashにリンクしている。

Ubuntuの/bin/shはbashではなくdash」によると、

ubuntu の /bin/sh は Almquist shell (ash) から派生したシェル Debian Almquist shell(dash) へのシンボリックリンクになっており、bash で OK な( … ) のシンタックスを dash は対応していないから。

Raspbianはubuntuと同様に/bin/shdashである。そのため、/bin/shbash感覚で使っていると、bashのシンタックスが使えないことがあるらしい。

まとめ

  • macOSではshbashはほぼ同じもの、結果としてshbashの実行結果が一致した。
  • Raspbianではshdashにシンボリックリンクが張られている。bashのシンタックス(ブレース展開{1..10})がdashでは対応していなかったため、結果としてshbashの実行結果が異なった。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?