www.wallcity.org | Zack Smith

Pull the Script Name in BASH

I very often use the Script Name as the log file name, it allows me to make the logs
always match up to the script even though the code is the same in all scripts
here is a quick example on how to do that

#!/bin/bash
declare -x SCRIPT_PATH="$0"
declare -x SCRIPT_NAME="${0##*/}"

echo $SCRIPT_PATH

echo $SCRIPT_NAME

A more raw example (Same Output):

#!/bin/bash
echo $0
echo ${0##*/}

Output:

/Users/acid/Dropbox/code/bash/scriptname.sh
scriptname.sh

Write a Comment