LoginSignup
1
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-02-10

こんにちは。
相対パスから絶対パスを得る簡単なシェルスクリプト関数を作りました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
1
0
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
1
0