LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

相対パスを絶対パスへ変換(シェルスクリプト)

こんにちは。
相対パスから絶対パスを得る簡単なシェルスクリプト関数を作りました1。なお絶対パスを与えた場合には入力をそのまま返します。

$ realpath .vimrc
/Users/user1/.vimrc
$ pwd
/Users/user1
$ realpath /temp
/temp
realpath() {
  case "$1" in /*) ;; *) printf '%s/' "$PWD";; esac; echo "$1"
}

他の方法

他の類似の方法としては、

  • realpath コマンドを使う(coreutils に含まれている)。
  • GNU 版 readlink コマンドを --canonicalize オプション指定で使う:
$ man greadlink
       -f, --canonicalize
              canonicalize by following every symlink in  every  component  of
              the  given  name  recursively;  all  but the last component must
              exist

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
What you can do with signing up
0