How to Get Bash Script Filename
- Using $0 to Get the Script Filename
-
Using
basename
to Get Only the Filename -
Using
dirname
to Get the Directory Name - Getting the Absolute Path of the Script
- Conclusion
- FAQ

When working with Bash scripts, knowing how to retrieve the script’s filename can be incredibly useful. Whether you need to log the script’s execution, manage files, or simply understand the context of your script, getting the filename is a fundamental task.
In this tutorial, we will explore various methods to obtain the filename of a Bash script. We’ll also touch on how this relates to Git, providing you with a comprehensive understanding that can enhance your scripting skills. So, let’s dive into the world of Bash scripting and learn how to efficiently get the filename!
Using $0 to Get the Script Filename
One of the simplest ways to get the filename of a Bash script is by using the special variable $0
. This variable holds the name of the script as it was called. Here’s a quick example of how to use it:
#!/bin/bash
echo "The script filename is: $0"
When you run this script, it will print the name of the script file as you executed it.
Output:
The script filename is: ./script_name.sh
In this example, $0
returns the name of the script, including the path if it was called from a different directory. This can be particularly useful when you want to log the execution of the script or when you need to reference the script itself within its logic. Keep in mind that if you execute the script from a different directory, the output will include the relative or absolute path, depending on how you invoked it.
Using basename
to Get Only the Filename
If you want to extract just the filename without the path, you can use the basename
command in conjunction with $0
. This method is handy when you need a clean filename for logging or processing purposes. Here’s how you can do it:
#!/bin/bash
filename=$(basename "$0")
echo "The script filename is: $filename"
Output:
The script filename is: script_name.sh
In this script, we use basename "$0"
to strip the path and get only the filename. This is particularly useful when you want to log or display the script’s name without any directory information. It helps keep the output clean and focused on the file itself, making it easier to read and understand, especially when dealing with multiple scripts in a project.
Using dirname
to Get the Directory Name
Sometimes, you might want to know not just the filename but also the directory where the script is located. The dirname
command can help with that. Here’s an example:
#!/bin/bash
dir=$(dirname "$0")
echo "The script is located in: $dir"
Output:
The script is located in: /path/to/script
In this case, dirname "$0"
returns the directory path of the script. This can be useful when your script needs to access other files or resources located in the same directory. Knowing the script’s location helps you manage relative paths effectively, which is crucial for maintaining the integrity of your scripts, especially in larger projects where file organization matters.
Getting the Absolute Path of the Script
If you need the absolute path of the script, you can combine realpath
with $0
. This is particularly useful in scenarios where you need to ensure that your script can find its resources regardless of the current working directory. Here’s how you can achieve that:
#!/bin/bash
absolute_path=$(realpath "$0")
echo "The absolute path of the script is: $absolute_path"
Output:
The absolute path of the script is: /home/user/scripts/script_name.sh
In this example, realpath "$0"
returns the absolute path of the script, which is essential for scripts that need to reference files or directories reliably. Using absolute paths can prevent errors that might occur when the script is executed from different locations, ensuring that your script runs smoothly and accesses the correct resources every time.
Conclusion
Retrieving the filename of a Bash script is a fundamental skill that can enhance your scripting capabilities. Whether you need the filename, the directory, or the absolute path, Bash provides simple yet powerful tools to achieve this. By mastering these techniques, you can create more robust scripts that are easier to manage and debug. As you continue to work with Bash and Git, these skills will undoubtedly come in handy, helping you streamline your workflow and improve your productivity. Happy scripting!
FAQ
- How can I get the filename of a script without the path?
You can use thebasename
command with$0
to get just the filename.
-
What does the
$0
variable represent in a Bash script?
The$0
variable contains the name of the script as it was called, including the path if applicable. -
Why would I need the absolute path of a script?
The absolute path is useful when your script needs to access other files reliably, regardless of the current working directory. -
Can I use these methods in Git hooks?
Yes, you can use these methods in Git hooks to log or manage script executions effectively. -
What is the difference between
dirname
andbasename
?
dirname
returns the directory path of a file, whilebasename
returns just the filename without any path information.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn