2

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.

Why is this happening? I also tried

$currentdir=`pwd` 

and that didn't work too.

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

xpwd() {
    $currentdir=$(pwd)
    echo "current dir is $currentdir"
}
Community
  • 1
  • 1
saksham bhatla
  • 21
  • 1
  • 1
  • 2

3 Answers3

1

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • No mention of how a) `$()` is preferable and b) there is an environment variable `$PWD` making the assignment kind of useless anyway? – Benjamin W. Feb 03 '16 at 22:49
  • @BenjaminW.: Thanks for telling me about `$PWD`; I wasn't aware of it. As for `$()`, I probably should have used that. – user2357112 Feb 03 '16 at 23:00
-1

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

currentdir=`pwd`
PBI
  • 335
  • 1
  • 4
-1

try:

currentdir=`pwd`

(remove the $)

jyvet
  • 2,021
  • 15
  • 22