Bootstrap FreeKB - Bash (Scripting) - scriptname $0
Bash (Scripting) - scriptname $0

Updated:   |  Bash (Scripting) articles

$0 can be used to get the name of the Bash script being invoked. For example, let's say you have a Bash script named foo.sh. Here is how you would show that foo.sh is the name of the script.

#!/bin/bash
echo scriptname = $0

 

Or, more commonly, scriptname is stored in a constant variable.

#!/bin/bash
SCRIPTNAME=$0
echo scriptname = $SCRIPTNAME

 

Which should return the following.

scriptname = /home/john.doe/foo.sh

 

If you just want the script name and not the full path to the script, basename can be used.

#!/bin/bash
SCRIPTNAME=$(basename $0)
echo scriptname = $SCRIPTNAME

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8ce4fb in the box below so that we can be sure you are a human.