CDPATHのcdコマンドの「代替ディレクトリ名」とは何ですか?

CDPATHのcdコマンドの「代替ディレクトリ名」とは何ですか?

CDとBashのヘルプページから:

The variable CDPATH defines the search path for the directory containing
DIR.  Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory.  If DIR begins
with a slash (/), then CDPATH is not used.

:しかし、「代替ディレクトリ」という概念を理解しておらず、コマンドでコロン()を使用する例が見つかりませんcd

ベストアンサー1

この変数はデフォルトでは設定されていませんが(少なくとも私が慣れているシステムでは)できる指定したターゲットディレクトリを検索するために別のディレクトリを有効にしますcd。たとえば、これを説明する方が簡単です。

$ echo $CDPATH    ## CDPATH is not set

$ cd etc          ## fails: there is no "etc" directory here
bash: cd: etc: No such file or directory
$ CDPATH="/"      ##CDPATH is now set to /
$ cd etc          ## This now moves us to /etc
/etc

つまり、デフォルトの動作は、cd foo「現在のディレクトリのサブディレクトリ、foo」というディレクトリに移動することです。または「ディレクトリ」は、CDPATHで指定された別のディレクトリにあります。CDPATH設定しないと、cd現在のディレクトリでのみ検索されますが、設定されている場合は、設定したディレクトリでも一致するものが見つかります。

コロンはとともに使用されずcd、 に設定するディレクトリを区切るために使用されますCDPATH

CDPATH="/path/to/dir1:/path/to/dir2:/path/to/dirN"

おすすめ記事