LoginSignup
5
4

More than 5 years have passed since last update.

mkdirしてcdするshell関数

Last updated at Posted at 2016-07-02

オプションをmkdirに渡したいなと思ったので、shell関数の勉強を兼ねて作成してみました。.bashrc等に書き込めば動きます

mkcd () {
  exec 3>&1

  cd "`
  if mkdir "$@" 1>&3; then
      while [ $# -gt 0 ]
      do
          case "$1" in
              -- ) printf '%s' "$2"; exit 0;;
              -* ) shift;;
              * ) printf '%s' "$1"; exit 0;;
          esac
      done
      printf '.'
      exit 0
  else
      printf '.'
      exit 1
  fi
  `"

  exec 3>&-
}

やりたかったこと

  • 実行時にオプションを指定できる
  • なるたけPOSIXの範囲内
  • 変数で環境を汚染しない

使用例

$ mkcd d
$ mkcd -vp p/c
$ mkcd d -- -v  # GNU
$ mkcd d -v     # BSD

参考文献

5
4
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
5
4