如何在 Unix shell 中将 pwd 定义为变量 [英] How to define pwd as a variable in Unix shell
问题描述
我尝试了以下方法,但没有奏效.
I tried the following but it didn't work.
dir=$pwd
echo $dir
/bin/env/####/ --id --edition dir- $dir
我想将当前工作目录粘贴到上述脚本中.
I want to paste the current working directory into the above script.
推荐答案
当前目录已经在一个名为 PWD
的变量中,它是 由shell自动设置:
The current directory is already in a variable, called PWD
, and it is automatically set by the shell:
echo "$PWD"
您还可以:
dir=$(pwd)
echo "$dir"
或者您可以在脚本中使用这些,而无需存储在其他变量中:
Or you could use these in your script without storing in additional variables:
/bin/env/####/ --id --edition-dir "$PWD"
/bin/env/####/ --id --edition-dir "$(pwd)"
供您参考:每次更改目录时,无论是在交互式 shell 还是脚本中,shell 都会将 PWD
变量的值设置为当前目录,并将 的值设置为OLDPWD
到上一个目录.
For your information: every time you change directory, whether in an interactive shell or a script, the shell sets the value of the PWD
variable to the current directory, and the value of OLDPWD
to the previous directory.
嗯,通常.正如 @WilliamPursell 指出的那样,OLDPWD
不是标准的,所以它可能不可用在所有壳中.
Well, usually. As @WilliamPursell pointed out, OLDPWD
is not standard, so it might not be available in all shells.
这篇关于如何在 Unix shell 中将 pwd 定义为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!