How to define pwd as a variable in Unix shell

给你一囗甜甜゛ 提交于 2019-12-06 05:23:17

问题


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.


回答1:


The current directory is already in a variable, called PWD, and it is automatically set by the shell:

echo "$PWD"

You could also:

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)"

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.

Well, usually. As @WilliamPursell pointed out, OLDPWD is not standard, so it might not be available in all shells.




回答2:


try this:

dir="$PWD"

or

dir="$(pwd)"

you may want to have double quotes too if your path contained special chars, like spaces.



来源:https://stackoverflow.com/questions/20839678/how-to-define-pwd-as-a-variable-in-unix-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!