使用 Bash 将当前目录保存在变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13275013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save current directory in variable using Bash?
提问by Stupid.Fat.Cat
What I'm trying to do is find the current working directory and save it into a variable, so that I can run export PATH=$PATH:currentdir+somethingelse
. I'm not entirely sure if they have a variable that contains cwd by default.
我想要做的是找到当前的工作目录并将其保存到一个变量中,这样我就可以运行export PATH=$PATH:currentdir+somethingelse
. 我不完全确定他们是否有一个默认包含 cwd 的变量。
How do I save the current directory in variable using Bash?
如何使用 Bash 将当前目录保存在变量中?
回答by sampson-chen
This saves the absolute path of the current working directory to the variable cwd
:
这会将当前工作目录的绝对路径保存到变量中cwd
:
cwd=$(pwd)
In your case you can just do:
在你的情况下,你可以这样做:
export PATH=$PATH:$(pwd)+somethingelse
回答by gerardw
I have the following in my .bash_profile:
我的 .bash_profile 中有以下内容:
function mark {
export =`pwd`;
}
so anytime I want to remember a directory, I can just type, e.g. mark there.
所以任何时候我想记住一个目录,我就可以输入,例如在那里标记。
Then when I want to go back to that location, I just type cd $there
然后当我想回到那个位置时,我只需在那里输入cd $there
回答by chepner
Your assignment has an extra $
:
你的任务有一个额外的$
:
export PATH=$PATH:${PWD}:/foo/bar
回答by mcalex
for a relativeanswer, use .
对于相对答案,请使用.
test with:
测试:
$ myDir=.
$ ls $myDir
$ cd /
$ ls $myDir
The first ls
will show you everything in the current directory, the second will show you everything in the root directory (/
).
第一个ls
将显示当前目录中的所有内容,第二个将显示根目录 ( /
) 中的所有内容。
回答by theme
One more variant:
另一种变体:
export PATH=$PATH:\`pwd`:/foo/bar
回答by simmerlee
You can use shell in-build variable PWD
, like this:
您可以使用 shell 内置变量PWD
,如下所示:
export PATH=$PATH:$PWD+somethingelse
回答by Piyush Sharma
current working directory variable ie full path /home/dev/other
当前工作目录变量,即完整路径 /home/dev/other
dir=$PWD
print the full path
打印完整路径
echo $dir