In the world of Bash scripting, there often arises the need to “get script name” or “get script path.” Whether you are building complex automation scripts or simple utilities, having the ability to determine the name of the script being executed and its directory or path is fundamental. These pieces of information enable you to make your scripts more versatile and adaptable to various environments and scenarios.
In this tutorial, we will delve into various methods for achieving these objectives in a Bash script. We’ll explore how to obtain the script name and its associated directory or path. This knowledge will empower you to write scripts that are not only functional but also robust and flexible.
Different methods to get script name in Bash
- Using
$0:$0holds the name of the script, which includes its path. To get just the name, further processing may be required. - Using
basename $0: Appliesbasenameto$0, extracting the base name of the script and stripping off the path information. - Using
${BASH_SOURCE[0]}:${BASH_SOURCE[0]}provides the source of the script, which can be used to determine the script name, even if sourced. - Using
$_(Limited Cases):$_gives the last argument to the previous command, and in some specific scenarios, it might provide the script name
1. Using $0 (Recommended)
Using $0 is a direct way to get the name of the script in a bash
script. This variable holds the name of the script along with the path
it was executed from.
#!/bin/bash
echo $0
In this example, $0 will output the name of the script with the
relative or absolute path, depending on how it is executed. For the bash
get script name without quotes, $0 is used directly without any
modification or command application.
2. Using basename $0
basename $0 will give you the base name of the script without any path
information. It extracts the script’s name directly, stripping away
directory information.
#!/bin/bash
script_name=$(basename $0)
echo $script_name
Here, basename $0 is used to get the bash name of the script without
any directory path, and it’s stored in the variable script_name, which
is then echoed.
3. Using ${BASH_SOURCE[0]}
${BASH_SOURCE[0]} holds the source name of the script. It is useful
especially when the script is sourced, and you want to get the name of
the script being sourced and not the parent script.
#!/bin/bash
echo ${BASH_SOURCE[0]}
${BASH_SOURCE[0]} will echo the bash get script name even when the
script is sourced. It directly gives the source script name without the
necessity of additional commands.
4. Using $_
$_ gives the last argument to the previous command. In certain
specific cases, it might hold the script name, but this is not a
reliable method.
#!/bin/bash
echo $_
$_ is a special variable in bash that holds the last argument of the
last executed command. Depending on the context in which it’s used, it
can behave differently.
When executing with ./script.sh the $_ variable holds the name
of the
script
as it was the last argument of the last command executed.
$ ./script.sh
./script.sh
When executing with bash script.sh the $_ variable holds the
path to the bash executable (/usr/bin/bash) as it was the command used
to execute the script.
$ bash script.sh
/usr/bin/bash
Different methods to get script directory or path in Bash
- Using
dirname $0:dirname $0provides the directory path where the script resides, excluding the script name itself. - Using
pwdand$0: By combiningpwdwith$0and other commands, you can get the absolute path of the script. - Using readlink and $0: The readlink command with various options helps get the absolute path, resolving symbolic links as well.
- Using
$(cd "$(dirname "$0")" && pwd): This command combination changes the directory to where the script is located and prints the working directory, giving the absolute directory path. - Using
pushdandpopdwith$0: Usingpushdandpopdtemporarily changes the directory to the script’s location, helping manage paths effectively. - Using
$PWDand$0: Utilizing the$PWDvariable with$0and further manipulations can yield the script’s absolute directory.
1. Using dirname $0
dirname $0 provides the directory path of the executing script. It
returns the path without including the script name itself.
#!/bin/bash
script_directory=$(dirname $0)
echo $script_directory
The command dirname $0 gives the directory of the script relative to
the current directory from which the script is executed. So, if you
execute the script from the directory where it is located, it will
output . (which represents the current directory).
To get the absolute path to the script’s directory, you can use a combination of commands to navigate to the script’s directory and then get the absolute path.
2. Using pwd and $0
Combining pwd with $0 and other commands can help to determine the
absolute directory path of the executing script.
#!/bin/bash
script_directory=$(pwd)/$(dirname $0)
echo $script_directory
pwd gives the current working directory, and when combined with
dirname $0, it provides a fuller path, contributing towards the aim to
bash get script directory.
3. Using readlink and $0
readlink command with various options can help get the absolute path
of the script, resolving symbolic links as well.
#!/bin/bash
script_directory=$(readlink -f $0)
echo $script_directory
readlink -f $0 provides the absolute path by resolving any symbolic
links, allowing us to bash get script path effectively, including the
script name.
4. Using $(cd "$(dirname "$0")" && pwd)
This method changes to the directory where the script is located, prints the working directory, and thus gives the absolute directory path of the script.
#!/bin/bash
script_directory=$(cd "$(dirname "$0")" && pwd)
echo $script_directory
By using cd "$(dirname "$0")" && pwd, the command first changes to the
script’s directory, then pwd prints the absolute path, achieving the
bash get script directory aim.
5. Using pushd and popd with $0
pushd and popd are used to change the directory temporarily to the
script’s directory, which helps in managing paths effectively.
#!/bin/bash
pushd "$(dirname "$0")" > /dev/null
script_directory=$(pwd)
popd > /dev/null
echo $script_directory
pushd "$(dirname "$0")" changes the directory to where the script is
located, pwd then gets the directory, and popd reverts back to the
original directory, hence allowing us to bash get script directory.
6. Using $PWD and $0
Utilizing the $PWD variable along with $0 and further manipulation
can yield the script’s absolute directory.
#!/bin/bash
script_directory=$PWD/$(dirname $0)
echo $script_directory
In this method, $PWD gives the current directory, and when
concatenated with dirname $0, it provides a path to bash get script
path relative to the current working directory.
Summary
In Bash scripting, there are various methods to obtain the script name and the script directory or path. Here are key takeaways from the methods discussed:
Key Takeaways:
$0is a straightforward way to get the script name.- Use
dirname $0for the script’s directory, but remember it’s relative to the current working directory. readlink -f $0is useful for obtaining the absolute path.$(cd "$(dirname "$0")" && pwd)ensures you get the absolute directory path.pushdandpopdcan be used for temporary directory changes.- Carefully consider the context in which you need the script name or path.
Further Reading:
- Bash Manual - Special Parameters:
Learn more about special parameters in Bash, including
$0and${BASH_SOURCE[@]}. - BashFAQ - 28: How do I determine the location of my script?: A comprehensive resource discussing various methods to get script location.
- Bash by Example - Chapter 1: Variables and Parameters: Covers script name and other variables in Bash.


