bash 如何获取变量以具有当前目录路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35189157/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:11:23  来源:igfitidea点击:

How to get a variable to have the current dir path?

bashscripting

提问by saksham bhatla

I have a really simple function in bash script that tells me the current directory. However it gives the error bash: =/home/users/abc: No such file or directory$currentdir is empty.

我在 bash 脚本中有一个非常简单的函数,它告诉我当前目录。但是它给出了错误bash: =/home/users/abc: No such file or directory$currentdir 为空。

Why is this happening? I also tried

为什么会这样?我也试过

$currentdir=`pwd` 

and that didn't work too.

这也不起作用。

This is a similar question Bash: current directory variableand I tried this but it didn't solve my problem.

这是一个类似的问题Bash: current directory variable,我试过了,但没有解决我的问题。

xpwd() {
    $currentdir=$(pwd)
    echo "current dir is $currentdir"
}

回答by user2357112 supports Monica

When you want to set a variable, you don't use the $:

当你想设置一个变量时,你不要使用$

currentdir=`pwd`

The $is for when you want to do a variable substitution.

$是,当你想要做一个变量替换。

回答by PBI

Assigning to a variable is without the '$' sign. Using the value is with the '$' sign.

分配给变量没有“$”符号。使用该值带有“$”符号。

currentdir=`pwd`

回答by jyvet

try:

尝试:

currentdir=`pwd`

(remove the $)

(删除$