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

10,305

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.

Share:
10,305
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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"
    }
    
  • Benjamin W.
    Benjamin W. about 8 years
    No mention of how a) $() is preferable and b) there is an environment variable $PWD making the assignment kind of useless anyway?
  • user2357112
    user2357112 about 8 years
    @BenjaminW.: Thanks for telling me about $PWD; I wasn't aware of it. As for $(), I probably should have used that.