Get the source directory of a Bash script from within the script itself

shell get script directory
bash get parent directory of script
bash get current directory
bash get path to script location
bash get absolute path
bash script path
bash get directory of file

How do I get the path of the directory in which a Bash script is located, inside that script?

For instance, let's say I want to use a Bash script as a launcher for another application. I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so:

$ ./application

The best compact solution in my view would be:

"$( cd "$( echo "${BASH_SOURCE[0]%/*}" )"; pwd )"

There is no reliance on anything other than Bash. The use of dirname, readlink and basename will eventually lead to compatibility issues, so they are best avoided if at all possible.

Reliable way for a Bash script to get the full path to itself, Here's what I've come up with (edit: plus some tweaks provided by sfstewman, levigroker, Kyle Strand, and Rob Kennedy), that seems to mostly fit my "better"  Get script full path, bash get script directory, bash get script path, bash get script name, shell get script name from inside the script, shell script get absolute path, Linux find script location. In my last script I shared the steps to run shell scripts in parallel and collect their individual exit status .

None of these worked for a bash script launched by Finder in OS X - I ended up using:

SCRIPT_LOC="`ps -p $$ | sed /PID/d | sed s:.*/Network/:/Network/: |
sed s:.*/Volumes/:/Volumes/:`"

Not pretty, but it gets the job done.

BASH Absolute path of current script · GitHub, The best compact solution in my view would be: "$( cd "$( echo "${​BASH_SOURCE[0]%/*}" )"; pwd )". There is no reliance on anything other than Bash. The use  A BASH script makes the first command line argument available as $1, the second as $2 and so on. The command run, excactly as it was called but without the command line arguments, is stored in $0. The dirname command. The dirname command returns the directory name part of a filename.

This worked for me when the other answers here did not:

thisScriptPath=`realpath $0`
thisDirPath=`dirname $thisScriptPath`
echo $thisDirPath

Get path of current script when executed through a symlink, If you want to also resolve any links to the script itself, you need a multi-line solution: This last one will work with any combination of aliases, source, bash -​c,  How do I get the path of the directory in which a Bash script is located, inside that script?. I want to use a Bash script as a launcher for another application. I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so:

You can do that just combining the script name ($0) with realpath and/or dirname. It works for Bash and Shell.

#!/usr/bin/env bash

RELATIVE_DIR_PATH="$(dirname "${0}")"
FULL_DIR_PATH="$(realpath "${0}" | xargs dirname)"
FULL_PATH="$(realpath "${0}")"

echo "FULL_PATH->${FULL_PATH}<-"

The output will be something like this:

# RELATIVE_PATH->./bin/<-
# FULL_DIR_PATH->/opt/my_app/bin<-
# FULL_PATH->/opt/my_app/bin/<-

$0 is the name of the script itself

An example:

determining path to sourced shell script, Try this as a general purpose solution: DIR="$(cd "$(dirname "$0")" && pwd)". In the specific case of following symlinks, you could also do this: DIR="$(dirname  I have several scripts of varying types (shell script, expect script, awk script) that I would like to run within 1 script.. They also take a command line argument (which it is getting successfully). The problem is, the parent script is exiting after the first script it calls is finished running.

Use a combination of readlink to canonicalize the name (with a bonus of following it back to its source if it is a symlink) and dirname to extract the directory name:

script="`readlink -f "${BASH_SOURCE[0]}"`"
dir="`dirname "$script"`"

What does 'source' do?, Is there a way for a sourced shell script to find out the path to itself? In tcsh , $_ at the beginning of the script will contain the location if the file was answer ${​BASH_SOURCE[0]} won't work if you try to find the path from within a function. Invoke it as source test1/test2/ or bash test1/test2/ . # ----- # This is a usermount script that reruns itself using sudo. # A user with the proper permissions only has to type # usermount /dev/fd0 /mnt/floppy # instead of # sudo usermount /dev/fd0 /mnt/floppy # I use this same technique for all of my #+ sudo scripts, because I find it convenient.

What is the difference between executing a Bash script vs sourcing it , source is a bash shell built-in command that executes the content of the file passed as If filename does not contain a slash, file names in PATH are used to find the If the sourced file is itself an executable script, then it will run, then return  That script will read in the input from the command line and substitute it as the destination directory at the target system, as well as the local directory that will be synced. It might look a bit complex as a final script, but each of the bits that you need to know to put it together are pretty simple.

How to Get the source directory of a Bash script within the script itself , Sourcing a script will run the commands in the current shell process. Use source if you want the script to change the environment in your currently The file can be in current directory or in a directory in $PATH . Sourcing a script means that it is parsed and executed by the current shell itself. I get something like: When writing bash scripts, you might want to get the directory that contains your script. There are multiple ways to accomplish that. Due to the flexibility of bash, some solutions work in some cases, but not in others.

When writing a bash script, how do I get the absolute path of the , This example will help you to get the full path of an directory using bash script within the script itself. Note: The above code, will give…

  • None of the current solutions work if there are any newlines at the end of the directory name - They will be stripped by the command substitution. To work around this you can append a non-newline character inside the command substitution - DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd && echo x)" - and remove it without a command substitution - DIR="${DIR%x}".
  • @jpmc26 There are two very common situations: Accidents and sabotage. A script shouldn't fail in unpredictable ways just because someone, somewhere, did a mkdir $'\n'.
  • anyone who lets people sabotage their system in that way shouldn't leave it up to bash to detect such problems... much less hire people capable of making that kind of mistake. I have never had, in the 25 years of using bash, seen this kind of thing happen anywhere.... this is why we have things like perl and practices such as taint checking (i will probably be flamed for saying that :)
  • @l0b0 Consider that you'd need the same protection on dirname, and that the directory could start with a - (e.g. --help). DIR=$(reldir=$(dirname -- "$0"; echo x); reldir=${reldir%?x}; cd -- "$reldir" && pwd && echo x); DIR=${DIR%?x}. Perhaps this is overkill?
  • I stronly suggest to read this Bash FAQ about the subject.
  • You probably should add slash to that: "$( cd "$( echo "${BASH_SOURCE[0]%/*}/" )"; pwd )". You'd have problems with root directory if you don't. Also why do you even have to use echo?
  • dirname and basename are POSIX standardized, so why avoid using them? Links: dirname, basename
  • Note that there's no ned to resort to echo. Simply invoking dirname will print the directory name. And, in this case, invoking echo without properly quoting the variable will potentially produce different output, since it will squash whitespace. Overall, cleaner to simply write dirname "$(realpath "$0")"
  • You are correct that this can be compressed into a single line. The breakdown into separate variables is for illustrative / self documenting purposes. The echo line is just there as p.o.c.
  • does this work with /bin/sh (instead /bin/bash )?
  • I'm pretty sure it does (if I remember correctly). Note that different nix distros and contexts like you bring up (and say Linux, vs Mac, vs Cygwin...bash vs dash...) handle all of this somewhat differently. Hence the reason there are SOO many answers on this thread! You'll need to test your use case to confirm. I wish there were a solid, cross platform/context answer to this simple question!