> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Get Script Directory Location in Bash
- URL: https://linuxhandbook.com/bash-directory-location/
- Published: 2023-08-20T03:14:34.000Z
- Updated: 2023-08-20T03:14:34.000Z
- Description: Here are a couple of ways of getting the directory location of your bash script inside the bash script.
- Author: Sagar Sharma
- Tags: Bash Tips

In a situation where your bash script needs to get the directory location where it is located? 

In this tutorial, I'll be sharing multiple scenarios and in the end, will give you a universal way to get the script directory location from the bash script itself.

## How to get script directory location in bash

There are multiple scenarios you need to keep in mind while writing a part of the script to get the location of the script:

- Path (relative or absolute)
- Whether the script is called through the symlink

So how do we get there? Let's have a look at step by step.

### 1\. Get the name of the script using `BASH_SOURCE[0]`

You might be wondering, why not use [the special variable $0](https://linuxhandbook.com/bash-dollar-0/) to do so? Well, there's a strong reason behind this.

There are two ways to execute the bash script:

- By sourcing it
- By executing it

**And when you use the `$0` variable in the script and execute it, it won't print the filename.**

As I intend to make a universal solution, I'm skipping using the $0 variable.

So here, I'll be using the `BASH_SOURCE[0]` variable which does the same job but gives more predictable results.

Let's start the script by printing the value of the `BASH_SOURCE` variable:

```shell
#!/bin/bash

echo "${BASH_SOURCE}"
```

Now, even if you source the file, it will give you the path and the filename.

For example, here, I created this file inside the `Test` directory, and when I sourced it from my home directory, it gave me this:

![get the filename of the script from the script itself in bash](https://linuxhandbook.com/content/images/2023/06/get-the-filename-of-the-script-from-the-script-itself-in-bash.png)

But if I change the directory where the file is located and then execute the file, then, it will only print the filename:

![prints the filename only when executed from the file directory](https://linuxhandbook.com/content/images/2023/06/prints-the-filename-only-when-executed-from-the-file-directory.png)

So in the next section, I will show you how you can get the absolute path irrespective of where the file was executed.

### 2\. Deal with symbolic links and get the absolute path

In case you don't know, symbolic links can behave as a shortcut to a file so if you created a symlink to a file then how it will handle the path?

Sounds complex? Let's have a look at the problem that we are trying to solve here.

Here, I have created a symbolic link inside my home directory pointing to `Test/location.sh`.

And when I executed the script via a symbolic link, it assumed that the file is in the current working directory (the home directory in my case):

![symbolic link pointing to the current working directory](https://linuxhandbook.com/content/images/2023/06/symbolic-link-pointing-to-the-current-working-directory.png)

But what if I tell you that you can [follow the symbolic link to its original file](https://linuxhandbook.com/follow-symbolic-link/) and get the absolute path at the same time?

Yes, you can deal with both of them at the same time.

For that purpose, I will be using the readlink command with the `-f` flag:

```
#!/bin/bash

Dir_path="$(readlink -f "${BASH_SOURCE}")"
echo "Script location is: $Dir_path"

```

Here, I have used the readlink and assigned the value to the `Dir_path` variable.

And in the end, used little statements which would make the output more human-readable:

![get the location of the script using the bash script itself](https://linuxhandbook.com/content/images/2023/06/get-the-location-of-the-script-using-the-bash-script-itself.png)

As you can see, from now on, you can use the symlinks too and it will [get the correct path](https://linuxhandbook.com/get-file-path/).

In a similar, related post, do you use the super useful brace expansions in your scripts?

[Using Brace Expansion in Bash ShellBrace expansion in the bash shell is a lesser known but an awesome feature. Learn about using them like a Pro Linux user with practical examples.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2022/11/bash-expansion-in-linux.png)](https://linuxhandbook.com/brace-expansion/)

Let me know if I was able to help you get the directory location in bash script.