Sil*_*137 13 command-line bash cd-command
在 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.
Run Code Online (Sandbox Code Playgroud)
但是我不理解“替代目录”的概念,并且找不到说明冒号 ( :) 与cd命令一起使用的示例。
ter*_*don 25
默认情况下未设置该变量(至少在我熟悉的系统中),但可以设置为使用不同的目录来搜索您提供的目标目录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
Run Code Online (Sandbox Code Playgroud)
换句话说,默认行为cd foo是“移动到名为‘foo’的目录中,该目录是当前目录或CDPATH 中给出的任何其他目录的子目录”。如果CDPATH没有设置,cd只会看在当前目录中,但是,当它被设置,它也将寻找任何你将其设置为目录的比赛。
冒号不与 一起使用cd,它用于分隔要设置的目录CDPATH:
CDPATH="/path/to/dir1:/path/to/dir2:/path/to/dirN"
Run Code Online (Sandbox Code Playgroud)
在手册中,CDPATH是这样描述的:
cd 命令的搜索路径。这是一个以冒号分隔的目录列表,shell 在其中查找 cd 命令指定的目标目录。示例值为“.:~:/usr”。
$~> mkdir /tmp/2 ./2 ./3
$~> cd 2
$~/2> cd ..
$~> CDPATH=/tmp
$~> cd 2
/tmp/2
$~> cd ~
$~> cd 3
$~/3>
Run Code Online (Sandbox Code Playgroud)
如您所见,在设置之后CDPATH=/tmp,Bash/tmp首先查找可能的目标目录。如果在 中找不到/tmp,它会尝试在当前目录中查找。我们还可以注意到(Shell Builtins)
如果使用 CDPATH 中的非空目录名,或者如果 - 是第一个参数,并且目录更改成功,则新工作目录的绝对路径名将写入标准输出。
我也想分享这个:
$~> CDPATH=.:/tmp
$~> cd 2
/home/myuser/2
$~/2> cd 2
/tmp/2
Run Code Online (Sandbox Code Playgroud)
在这个延续中,CDPATH已经给出了两个目录。第一个是.,即当前目录。由于它首先出现,在尝试后cd 2,我们转到/home/myuser/2,虽然/tmp/2也存在。就像$PATH,第一个列出的目录优先。
| 归档时间: |
|
| 查看次数: |
979 次 |
| 最近记录: |