Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | N/A |
- basename command – Display filename portion of pathname.
- dirname command – Display directory portion of pathname.
- Bash parameter substitution.
- $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/foo.sh:
dirname /home/nixcraft/scripts/foo.sh
Sample outputs:
/home/nixcraft/scripts
The following line sets the shell variable i to /home/nixcraft/scripts:
i=`dirname /home/nixcraft/scripts/foo.sh` echo "$i"
OR
i=$(dirname /home/nixcraft/scripts/foo.sh) echo "$i"
In bash script use $0 instead of /home/nixcraft/scripts/foo.sh:
#!/bin/bash script="$0" basename="$(dirname $script)" echo "Script name $script resides in $basename directory."
Sample outputs:
Script name /tmp/test.sh resides in /tmp directory.
Using bash shell ${var%pattern} syntax
To Remove from shortest rear (end) pattern use the following syntax:
var=${path%/*}
For example:
x="/Users/nixcraft/scripts/bar.sh" echo "${x%/*}" y="${x%/*}" echo "$y"
An updated version of the above script:
#!/bin/bash # Purpose : Linux / Unix shell script find out which directory this script file resides # Author : nixCraft <http://www.cyberciti.biz> under GPL v2.x+ # ------------------------------------------------------------------------------------- script="$0" basename="${script%/*}" config1="${basename}/.backup" config2="${basename}/.ignore" config3="${basename}/.target" echo "Script name $script resides in $basename directory." echo "Reading config file $config1 $config2 $config3, please wait..."
Run it as:
$ chmod +x /tmp/test.sh
$ /tmp/test.sh
Sample outputs:
Fig.01 Sample run from test.sh
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:
#!/bin/bash # Purpose : Linux / Unix shell script find out which directory this script file resides # Author : nixCraft <http://www.cyberciti.biz> 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:
./demo.bash cd /home/vivek/ ../../tmp/demo.bash /tmp/demo.bash
Sample outputs:
See also
- See man pages for more info – bash(1)
🐧 11 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
i found aund used the function below. This should be included in the script itself.
Further down in the script I can use $(getScriptPath) as variable, such as
Hope this helps:
Russ, I appreciate that you are sharing your code with us.
Thank you for this!
Hello,
The problem is that method won’t give your the absolute path if it is started with the relative one.
For example, if my script is launched with ./test.sh I won’t know from the script where am I really residing in the FS.
So the response is :
abspath=$(readlink -f $0)
Then you can do any basename, dirname an other path manipulation on it.
Thanks for the heads up. The faq has been updated. I appreciate your post.
If a script needs to know where it is located, it is badly designed.
Chris,
Can you tell me why would it be badly designed? Jugement without deep knowledge about the facts are useless.
Nicolas
I feel if a script has to be edited when moved from directory to directory to operate correctly it is badly designed.
If a script has to be moved from directory to directory, it is badly designed. Scripts should always be placed in a directory in your PATH variable, so they can be called from anywhere.
readlink -f $0 won’t work on Mac OS X.
A nice one-liner would be
It doesn’t resolve the real path, though. For this you’ll need an OS check or something…
Note that OS X’s readlink is not reliable.
What about: