Linux / Unix: Shell Script Find Out In Which Directory Script File Resides

I need to find out in which directory my bash script resides so that I can read config file called .backup .ignore .target. For example, if my script resides in >/home/foo/, I need to read /home/foo/.{backup,ignore,target} files. How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems?

You can use any one of the following method to find out the portion of pathname:

  1. basename command – Display filename portion of pathname.
  2. dirname command – Display directory portion of pathname.
  3. Bash parameter substitution.
  4. $0 expands to the name of the shell or shell script.

Examples: Shell script find out which directory the script file resides

The following example display directory path or portion of /home/nixcraft/scripts/

dirname /home/nixcraft/scripts/

Sample outputs:


The following line sets the shell variable i to /home/nixcraft/scripts:

i=`dirname /home/nixcraft/scripts/`
echo "$i"


i=$(dirname /home/nixcraft/scripts/
echo "$i"

In bash script use $0 instead of /home/nixcraft/scripts/

basename="$(dirname $script)"
echo "Script name $script resides in $basename directory."

Sample outputs:

Script name /tmp/ resides in /tmp directory.

Using bash shell ${var%pattern} syntax

To Remove from shortest rear (end) pattern use the following syntax:


For example:

echo "${x%/*}"
echo "$y"

An updated version of the above script:

# Purpose : Linux / Unix shell script find out which directory this script file resides
# Author : nixCraft <> under GPL v2.x+
# -------------------------------------------------------------------------------------
echo "Script name $script resides in $basename directory."
echo "Reading config file $config1 $config2 $config3, please wait..."

Run it as:
$ chmod +x /tmp/
$ /tmp/

Sample outputs:

Fig.01 Sample run from

A note about finding physical or real path

You may not get a real physical path and real path may be a symbolic link. To get physical path use realpath command. The realpath command uses the realpath() function to resolve all symbolic links, extra / characters and references to /./ and /../ in path. This is useful for shell scripting and security related applications.

Another recommended option is to use the readlink command to display value of a symbolic link or canonical file name:

# Purpose : Linux / Unix shell script find out which directory this script file resides
# Author : nixCraft <> under GPL v2.x+
# -------------------------------------------------------------------------------------
## Who am i? ##
## Get real path ##
_script="$(readlink -f ${BASH_SOURCE[0]})"
## Delete last component from $_script ##
_mydir="$(dirname $_script)"
## Delete /path/to/dir/ component from $_script ##
_myfile="$(basename $_script)"
echo "Script : $_script"
echo "Directory portion of $_script : $_mydir"
echo "Filename portion of $_script : $_myfile"

Save and close the file. Run it as follows:

cd /home/vivek/

Sample outputs:

Fig.02: Finding real path

See also
  • See man pages for more info – bash(1)

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.