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

シェルスクリプトで実行しているスクリプトのパスを取得する

0
Posted at

シェルスクリプトを作成していると実行しているスクリプト自身の絶対パスを取得したくなる時がある。
そんな時は以下のようにするとよい 。

#!/bin/bash
SCRIPT_DIR=$(cd $(dirname $0) ; pwd)/
echo "${SCRIPT_DIR}"

説明

dirname $0

スクリプトのあるディレクトリの相対パスを取得する。

$(cd $(dirname $0)

スクリプトのあるディレクトリに移動する。

SCRIPT_DIR=$(cd $(dirname $0) ; pwd)

カレントディレクトリがスクリプトのディレクトリになるのでpwdでカレントディレクトリのパスを取得する。
コマンド置換はサブシェル環境で実行されるのでスクリプトディレクトリ取得後にカレントディレクトリは変更されない。

参考

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?