2
2

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

いくつかのOSで実験したけれど、POSIX準拠ならこれで十分だった。

#!/bin/sh

case "$0" in
    ./*) SCRIPTNAME="$(pwd)/${0#./}";;
    /*)  SCRIPTNAME="$0";;
    *)   SCRIPTNAME="$(pwd)/$0";;
esac
SCRIPTDIR="${SCRIPTNAME%/*}"

echo $0
echo $SCRIPTNAME
echo $SCRIPTDIR

Solaris の /bin/sh は、おそらくSystem V 由来の、POSIX 以前の Bourne shell なので、これじゃないとだめだった。

#!/bin/sh

case "$0" in
    ./*) SCRIPTNAME="`pwd`/$0";;
    /*)  SCRIPTNAME="$0";;
    *)   SCRIPTNAME="`pwd`/$0";;
esac
SCRIPTDIR="`dirname \"$SCRIPTNAME\"`"

echo $0
echo $SCRIPTNAME
echo $SCRIPTDIR
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?