How to Get Current Directory in Shell Script

How to Get Current Directory in Shell Script

Often Linux developers and system administrators need to determine present working directory, either in terminal or from within a shell script. This is mainly required if you want to work with files and file locations in your shell script. There are a couple of simple ways to do this using pwd Linux command or using PWD environment variable. In this article, we will look at both these methods to get current directory in shell script. You can run these commands on terminal also.

How to Get Current Directory in Shell Script

First we will look at how to obtain value of current directory using pwd command as well as PWD environment variable. All these commands are readily available in almost every Linux distribution so you do not need to install or configure anything.

$ pwd
/home/ubuntu


$ echo $PWD
/home/ubuntu

The first line above uses pwd Linux command to get present working directory. The third line uses echo command to display value of PWD environment variable which also stores the value of present working directory.

You can use either of these commands directly in terminal to get value of current directory.

Now we will look at how to use these commands inside a shell script.

1. Create Shell Script

Open terminal and run the following command to create an empty shell script.

$ vi current_dir.sh

2. Get Current Directory

Add the following lines to your shell script.

!/bin/sh

echo "current directory using pwd command"
echo $(pwd)

echo "current directory using PWD variable"
echo $PWD
curr_dir=$PWD
another_dir='/data'
sub_dir=$curr_dir$another_dir

echo "sub directory of present working directory"
echo $sub_dir

The first line above sets the execution environment. Next, we display the current working directory using pwd command. Then we use echo command to display the value of current directory stored in PWD environment variable. We also store this value in another variable curr_dir, in case you need to use it later in your shell script.

For user’s benefit, we have also defined a string another_dir and concatenated it with the present working directory stored in curr_dir to generate a sub directory path of current directory.

If you want to view the value of previous working directory, it is stored in OLDPWD environment variable, and you can echo it, just as you echoed PWD environment variable. Save and close the file.

3. Make Shell Script Executable

Run the following command to make your shell script executable. We use chmod command to add ‘x’ executable permission to the file.

$ sudo chmod +x current_dir.sh

4. Test Shell Script

Lastly, you can run the shell script using the command shown below.

$ ./current_dir.sh

You will see the following output.

current directory using pwd command
/home/ubuntu

current directory using PWD variable
/home/ubuntu

In this article, we have learnt how to get current working directory in shell script using pwd command as well as using PWD environment variable. You can use either of them depending on your requirement. You can also concatenate additional strings to them to generate different file paths.

Also read:

How to Get Current Directory in Python
How to Iterate over Multiple Python Lists
How to Check Dependencies for Linux Package
How to Install Specific Version of NPM Package
How to Disable GZIP Compression in Apache Server

Leave a Reply

Your email address will not be published. Required fields are marked *