Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save current directory in variable using Bash?

People also ask

How do I get the current working directory in a variable in Bash?

The current working directory is conveniently stored in the variable $PWD which is available in all POSIX compliant shells. Another option to get the current working directory is by using the pwd command. That pwd command is a shell builtin and available in all POSIX compliant shells as well.

What is $$ variable in Bash?

$$ is a Bash internal variable that contains the Process ID (PID) of the shell running your script. Sometimes the $$ variable gets confused with the variable $BASHPID that contains the PID of the current Bash shell.

How do you set a variable in Bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


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

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


Your assignment has an extra $:

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

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


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

dir=$PWD

print the full path

echo $dir