Script to copy a directory path in memory in Bash terminal

Introduction

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.

Prerequistis

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

Copy path (cppath)

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

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

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

Now start a terminal and run

cppath

The current directory is stored in the cppath.sh 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 cppath.sh is pasted into $cppath variable again and you have access to that path. So you can do

cd $cppath
#or
cp /home/blah/blah $cppath

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

Tags ➡ Linux HPC

Subscribe

I notify you of my new posts

Latest Posts

Comments

0 comment