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.

Example

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.

Example

dirname bash.sh 

dirname filename

dirname Options

The following options can be used 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 the 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 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 is shown 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:

#!/bin/bash
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:

./bash.sh

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

run bash script

Conclusion

We discussed in this article what the purpose of dirname $0 is 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.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ivar
ivar
2 years 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 &&

nestor
nestor
1 year ago

Thank you for the information. I have never used dirname before.
To get the above bash.sh to run, I had to change the second line to either:
Directory=$(cd $(dirname $0) ; pwd -P)
or
Directory=$(cd dirname $0 ; pwd -P)
[those are ticks, not quotes. Sometimes hard to tell the difference on a monitor.]

nestor
nestor
1 year ago

Thank you for the information. I have never used dirname before.
To get the above bash.sh to run, I had to change the second line to either:
Directory=$(cd $(dirname $0) ; pwd -P)
or
Directory=$(cd `dirname $0` ; pwd -P)
[those are backquotes, not single quotes. Sometimes hard to tell the difference on a monitor.]

You May Also Like