Save current directory in variable using Bash?

后端 未结 9 659
时光说笑
时光说笑 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 16:02

    On a BASH shell, you can very simply run:

    export PATH=$PATH:`pwd`/somethingelse
    

    No need to save the current working directory into a variable...

    0 讨论(0)
  • 2020-12-07 16:03

    This saves the absolute path of the current working directory to the variable cwd:

    cwd=$(pwd)
    

    In your case you can just do:

    export PATH=$PATH:$(pwd)+somethingelse
    
    0 讨论(0)
  • 2020-12-07 16:06

    Similar to solution of mark with some checking of variables. Also I prefer not to use $variable but rather the same string I saved it under

    save your folder/directory using save dir sdir myproject and go back to that folder using goto dir gdir myproject

    in addition checkout the workings of native pushd and popd they will save the current folder and this is handy for going back and forth. In this case you can also use popd after gdir myproject and go back again

    # Save the current folder using sdir yourhandle to a variable you can later access the same folder fast using gdir yourhandle
    
    function sdir {
        [[ ! -z "$1" ]] && export __d__$1="`pwd`";
    }
    function gdir {
        [[ ! -z "$1" ]] && cd "${!1}";
    }
    

    another handy trick is to combine the two pushd/popd and sdir and gdir wher you replace the cd in the goto dir function in pushd. This enables you to also fly back to your previous folder when making the jump to the saved folder.

    # Save the current folder using sdir yourhandle to a variable you can later access the same folder fast using gdir yourhandle
    
    function sdir {
        [[ ! -z "$1" ]] && export __d__$1="`pwd`";
    }
    function gdir {
        [[ ! -z "$1" ]] && pushd "${!1}";
    }
    
    0 讨论(0)
提交回复
热议问题