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
Table of Contents
What is dirname $0 in shell?
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.
$0 parameter contains the name of the shell script.
dirname command – Truncate the parent directory path from a string
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
dirname command displays the argument PATH and removes the last file or directory suffix.
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.
The following options, we can use with the
-z or – zero – These options separate the output by a
NULL character instead of initiating a
dirname -z PATH
As you can see in the above output, lines are not separated from new lines.
[pompt] dirname --help [/prompt]
--help – This option prints help information.
--version – This option prints the version details.
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.shThe following output shows on the terminal:
Usage of dirname $0 in bash code
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 -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.
Using the following command run the bash script in the terminal:
When you will run the bash script, you will get the following output:
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