LoginSignup
22
17

More than 5 years have passed since last update.

カレントディレクトリを変更せずに、特定のディレクトリでコマンドを実行する

Posted at

概要

cd してコマンドを実行して cd - で1つ前のディレクトリに戻る」という作業を
1行でできるので便利!

$ pwd
/Users/quanon

$ (cd /etc/apache2 && ls -l)
total 64
drwxr-xr-x  13 root  wheel    442  8 25  2013 extra
-rw-r--r--   1 root  wheel  18121 11  1  2013 httpd.conf
-rw-r--r--   1 root  wheel  12958 11  1  2013 magic
-rw-r--r--   1 root  wheel  53258 11  1  2013 mime.types
drwxr-xr-x   4 root  wheel    136  8 25  2013 original
drwxr-xr-x   3 root  wheel    102  8 25  2013 other
drwxr-xr-x   3 root  wheel    102  2  9 18:33 users

$ pwd
/Users/quanon

原理

(command) のようにコマンドを丸括弧でくくって実行すると、
サブシェル (別プロセス) で実行したことになる。
そして、カレントディレクトリはプロセスごとに管理されているので、
サブシェルで cd しても、もとのシェルには影響しない。

ということらしいです。

参考

22
17
1

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
22
17