0
1

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 5 years have passed since last update.

シェルスクリプトについてメモ

Posted at

シェルスクリプトについてメモ。

シバンについて #!

シェルスクリプトを作成する際は、
冒頭に #! で始まる行を追加する必要がある。これはシバンと呼ばれている。

./test.sh
# !/bin/bash
mkdir test

#!/bin/bash という行は、/bin/bashで動かすという宣言をしている

このシェルスクリプトの実行方法は、こちら

./test.sh

シェルから実行命令を受けたLinuxカーネルが、対象ファイルの先頭を確認して
もし#!があったら、その後に書かれたコマンドを実行するよという流れ

実質的には

/bin/bash ./test.sh

このようなコマンドラインとして実行される

sourceコマンドでファイルを実行

その他にsourceコマンドを利用して、シェルスクリプトを実行することができる。

source ./test.sh

sourceコマンドは、指定したファイルの内容をそのままコマンドラインとして実行する。

シバン(#!) が表記されていても、# はコメントアウトとして扱われるので無視される

ちなみにsourceコマンドと同様の意味をもつ、ドットコマンドもある

. ./test.sh

ドットだと、見落としやすいので、sourceコマンドの方がベターかもしれない。

こういう基本はしっかりと抑えたい。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?