What Is dirname $0 and Usage Examples

dirname 1

In this tutorial, we’ll explore the dirname command and how to use dirname $0 to get the location of the running bash script. Furthermore, we will discuss how to retrieve an absolute path using the dirname command.

What is dirname $0 in shell?

The dirname $0 command returns the directory where the Bash script file is saved.  We can return a relative path or an absolute path. This all depends on how the bash script is called.

The $0 parameter contains the name of the shell script.

dirname command – Truncate the parent directory path from a string

The dirname is the short form of “directory name”. It is a built-in command in Linux and Unix-like operating systems.  Given a string containing the path of a file or directory, this command will return the parent directory’s path.

Syntax of dirname command

The following syntax is commonly used for running the dirname command on the Linux terminal:

dirname [OPTIONS] PATH

The dirname command displays the argument PATH and removes the last file or directory suffix.


dirname /home/sam-pc/Documents/bash.sh 

dirname command

If the argument PATH only contains a string without any slashes (/),  then, in a case, the dirname command prints only the single dot . in the output – a relative path, the directory you are in.


dirname bash.sh 

dirname filename

dirname Options

The following options, we can use with the dirname command:

-z or – zero – These options separate the output by a NULL character instead of initiating a new line.

dirname -z PATH 

dirname z option

As you can see in the above output, lines are not separated from new lines.

--help – This option prints help information.

[pompt] dirname --help [/prompt]

dirname help

--version – This option prints the version details.

dirname --version 

dirname – version

Handle multiple files using dirname

Using the dirname, you can handle the multiple files. Pass the absolute path of each file one after another file as follows:

dirname /home/sam-pc/Documents/bash.sh /home/sam-pc/Downloads/abc.sh 
The following output shows on the terminal:

dirname handle mutiple files

Usage of dirname $0 in bash code

The dirname $0 can be used in a bash script. So, create a bash file and paste the following code:

Directory=$(cd 'dirname $0' > && pwd -P)
echo $Directory

In the above bash code, dirname $0 retrieve the directory where the bash script is located. If the bash script is located in the same from where we are running the script, it will return the relative path “.”

To get the absolute path of our script regardless of where we are running it (same directory or different one), we use the pwd command.

The pwd -P is the absolute path to the current directory. In the above code, the script’s directory name is a variable that we can use later.

If you want to return the absolute file path where the bash script is located, using cd we will change the bash script’s directory that returns the dirname, and then it will save into a variable before echoing the directory.

dirname $0 in bash script

Using the following command run the bash script in the terminal:


When you will run the bash script, you will get the following output:

run bash script


We discussed in this article what is the purpose of dirname $0 and how you can use this in bash code. Moreover, we go through the different options of the dirname command. We also demonstrate how to handle multiple files using the dirname command.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 months ago

Good one… (y), it helped me to understand more in detail about the dirname and $0

Note: I see a correction required in night mode code line 2 “>” is not required before &&

You May Also Like
Bash Write to File
Read More

Bash Write to File

Reading and writing to files are common tasks among Linux command-line users. There are two ways in bash…