10+ Simple Methods to get Script Name and Path in Bash


Shell Scripting, Linux

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

  1. Using $0: $0 holds the name of the script, which includes its path. To get just the name, further processing may be required.
  2. Using basename $0: Applies basename to $0, extracting the base name of the script and stripping off the path information.
  3. 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.
  4. 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

  1. Using dirname $0: dirname $0 provides the directory path where the script resides, excluding the script name itself.
  2. Using pwd and $0: By combining pwd with $0 and other commands, you can get the absolute path of the script.
  3. Using readlink and $0: The readlink command with various options helps get the absolute path, resolving symbolic links as well.
  4. 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.
  5. Using pushd and popd with $0: Using pushd and popd temporarily changes the directory to the script’s location, helping manage paths effectively.
  6. Using $PWD and $0: Utilizing the $PWD variable with $0 and 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:

  • $0 is a straightforward way to get the script name.
  • Use dirname $0 for the script's directory, but remember it's relative to the current working directory.
  • readlink -f $0 is useful for obtaining the absolute path.
  • $(cd "$(dirname "$0")" && pwd) ensures you get the absolute directory path.
  • pushd and popd can be used for temporary directory changes.
  • Carefully consider the context in which you need the script name or path.

Further Reading:

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

4 thoughts on “10+ Simple Methods to get Script Name and Path in Bash”

  1. $BASH_SOURCE is not portable (if the shell is /bin/sh, /bin/ksh etc ?? ) .

    So it appears there is no generic Unix shell method of getting the
    dir D in which a script S resides, when invoked as : . D/S

    Reply
    • $(dirname "$(readlink -f "$BASH_SOURCE")") seems to work.

      [root@server ~]# . /tmp/dir1/dir2/dir3/dir4/script.sh
      Script Path 3: /tmp/dir1/dir2/dir3/dir4

      Or a better way would be to invoke using sh

      ~]# sh /tmp/dir1/dir2/dir3/dir4/script.sh
      Reply

Leave a Comment