Save current directory in variable using Bash?

后端 未结 9 658
时光说笑
时光说笑 2020-12-07 15:34

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

相关标签:
9条回答
  • 2020-12-07 15:46

    for a relative answer, 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 (/).

    0 讨论(0)
  • 2020-12-07 15:51

    You can use shell in-build variable PWD, like this:

    export PATH=$PATH:$PWD+somethingelse
    
    0 讨论(0)
  • 2020-12-07 15:53

    I have the following in my .bash_profile:

    function mark {
        export $1=`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

    0 讨论(0)
  • 2020-12-07 15:56

    One more variant:

    export PATH=$PATH:\`pwd`:/foo/bar
    
    0 讨论(0)
  • 2020-12-07 16:01

    Your assignment has an extra $:

    export PATH=$PATH:${PWD}:/foo/bar
    
    0 讨论(0)
  • 2020-12-07 16:02

    current working directory variable ie full path /home/dev/other

    dir=$PWD
    

    print the full path

    echo $dir
    
    0 讨论(0)
提交回复
热议问题