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?

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 bash 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, 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:

#!/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 will run the bash script, you will get the following output:

run bash script

Conclusion

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.

1 Shares:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
You May Also Like