Script to copy a directory path in memory in Bash terminal


Here I show how to copy the path of current directory in memory in a Bash terminal, without a mouse, and use it in the current terminal or any new terminal opened afterwards. This is like when on Windows (or any other Desktop OS), you copy a path and then somewhere else you paste it. You can also use that path to navigate back there quickly, copy some content to it from somewhere else and so on.


I am assuming you are familiar with Bash terminal and its commands.

Copy path (cppath)

Create ~/ file and add the below lines into your ~/.bashrc

source ~/
alias cppath='cppath=$PWD && echo cppath=$PWD > ~/'

Note that single quotation marks delay the expansion of cppath alias, so don’t use "” there.

Now start a terminal and run


The current directory is stored in the file and $cppath variable that is available in the current terminal. Moreover, if you open a new terminal, .bashrc is run and the content of is pasted into $cppath variable again and you have access to that path. So you can do

cd $cppath
cp /home/blah/blah $cppath

To sum up, cppath command copies the current directory path into $cppath variable.

Tags ➡ Linux HPC


I notify you of my new posts

Latest Posts


0 comment