LoginSignup
9
3

More than 3 years have passed since last update.

【Ubuntu】シェルスクリプトの実行方法

Last updated at Posted at 2020-11-02
$bash ./test.sh
$sh ./test.sh
$ ./test.sh

パターン1

スクリプトの中身

$ cat test.sh
echo "test!";

実行(成功)

$ sh test.sh
test!

パターン2

スクリプトの中身

$ cat test.sh
#!/bin/bash

array=(1 2 3);
echo "test!";

L3にbashでしか使えない、配列の記法があります。

実行(失敗)

$ sh test.sh
test.sh: 3: test.sh: Syntax error: "(" unexpected

スクリプトの先頭に #!/bin/bash を入れているのに、何故かbashが効いていないせいで怒られます。

実行(失敗)2

ファイル名の先頭に ./ を入れてみる

$ sh ./test.sh
./test.sh: 3: ./test.sh: Syntax error: "(" unexpected

それでも怒られます。

実行(成功)

$ ./test.sh 
test!

sh を使わないでファイルだけ指定するか、 bash を使うと実行できます。

$ bash test.sh
test!

ちなみに

$ test.sh 
test.sh: command not found

./ を付けないと、コマンドだと思われて怒られます。


追記

@hidezzz さんのコメントを参考に、bashコマンドを追記しました。ご教授有難うございました!

9
3
2

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
9
3