176

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • 1
    so to save it to a variable would I do mydir=. ? – Stupid.Fat.Cat Nov 07 '12 at 17:33
  • 1
    yup just tested with mydir=. then ls $mydir -but it's relative so it will be the pwd from wherever you run it – mcalex Nov 07 '12 at 17:34
  • Oh, I had some spaces. t.t ${PWD} should've worked. haha. ... whoops – Stupid.Fat.Cat Nov 09 '12 at 14:20
  • Possible duplicate of [Getting current path in variable and using it](https://stackoverflow.com/q/1636363/608639), [How to get a variable to have the current dir path?](https://stackoverflow.com/q/35189157/608639), etc. – jww Dec 22 '19 at 13:29

10 Answers10

275

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
peakxu
  • 6,667
  • 1
  • 28
  • 27
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
36

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

gerardw
  • 5,822
  • 46
  • 39
  • Works well but for paths with spaces, e.g. "Google Drive" you have to do ```cd "$foo"``` for the whole path to be read – mercergeoinfo Jun 10 '16 at 09:20
  • 3
    To get around the problem with spaces in the path change ```export $1=`pwd` ``` to ```export $1="`pwd`" ``` and then when calling such paths use ```cd "$foo"``` instead of just ```cd $foo``` – mercergeoinfo Jun 10 '16 at 09:39
  • mercergeoinfo: Strictly speaking, the quotes in `export $1="\`pwd\`"` are not needed, since word splitting does not happen at the right-hand side of an assignment, although quoting of expansions is good to get used to as a habit. – Larry Jun 06 '18 at 07:39
  • 2
    See also [`cdable_vars`](https://stackoverflow.com/a/39839346/1518546) to type just `cd there` – John Cummings Apr 24 '19 at 14:59
11

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

dir=$PWD

print the full path

echo $dir
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Piyush Sharma
  • 591
  • 7
  • 9
5

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

mcalex
  • 6,628
  • 5
  • 50
  • 80
5

Your assignment has an extra $:

export PATH=$PATH:${PWD}:/foo/bar
chepner
  • 497,756
  • 71
  • 530
  • 681
3

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

One more variant:

export PATH=$PATH:\`pwd`:/foo/bar
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
theme
  • 361
  • 5
  • 7
0

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

export PATH=$PATH:$PWD+somethingelse
simmerlee
  • 111
  • 1
  • 6
0

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}";
}
snh_nl
  • 2,877
  • 6
  • 32
  • 62
0

With hint from @gerardw ans I added the following in .bashrc, this has persistence also:

function mark {
  cur=`pwd`;
  echo "export cur_project=$cur" > ~/.cur_project
  . ~/.cur_project
}
. ~/.cur_project

alias curp='cd $cur_project'

So cur_project variable is available in .bashrc or bash command any time.

This alias curp='cd $cur_project' is just in case someone wants to switch to current project any time.

Gagan
  • 1,267
  • 3
  • 18
  • 28