Skip to content

How to Find Script file name in a Bash Script

How to Find Script file name in a Bash Script. Check own script named inside bash script. How to find self script named in bash script.

Command

SNAME=$(basename “$(test -L “$0” && readlink “$0” || echo “$0″)”)

While running a bash script using Linux terminal the script name can also passed as argument at $0. but if we are using full path of script to execute, it will show full path as results when print value of $0. So use above command will exactly provide only script name. It also handle if you are running any bash script using symbolic link.

Example:

Create a script named test.sh and add following content in script.

#!/bin/bash

SNAME=$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")
echo "Your Script Name is $SNAME"

Lets execute this script and check for results.

 # sh test.sh

Result will be as follows:

Your Script Name is test.sh

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top