Save current directory in variable using Bash?

BashVariablesDirectory

Bash Problem Overview


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 entirely sure if they have a variable that contains cwd by default.

How do I save the current directory in variable using Bash?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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

Solution 3 - Bash

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

dir=$PWD

print the full path

echo $dir

Solution 4 - Bash

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 (/).

Solution 5 - Bash

Your assignment has an extra $:

export PATH=$PATH:${PWD}:/foo/bar

Solution 6 - Bash

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...

Solution 7 - Bash

One more variant:

export PATH=$PATH:\`pwd`:/foo/bar

Solution 8 - Bash

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

export PATH=$PATH:$PWD+somethingelse

Solution 9 - Bash

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}";
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStupid.Fat.CatView Question on Stackoverflow
Solution 1 - Bashsampson-chenView Answer on Stackoverflow
Solution 2 - BashgerardwView Answer on Stackoverflow
Solution 3 - BashPiyush SharmaView Answer on Stackoverflow
Solution 4 - BashmcalexView Answer on Stackoverflow
Solution 5 - BashchepnerView Answer on Stackoverflow
Solution 6 - BashJulian - BrainAnnex.orgView Answer on Stackoverflow
Solution 7 - BashthemeView Answer on Stackoverflow
Solution 8 - BashsimmerleeView Answer on Stackoverflow
Solution 9 - Bashsnh_nlView Answer on Stackoverflow