Monday, October 14, 2013

Reading self path/name/parameter from bash

Below is script and test run to get parameter,path,script file name etc.

#!/bin/bash

echo
echo "arguments called with ---->  ${@}     "
echo "\$1 ---------------------->  $1       "
echo "\$2 ---------------------->  $2       "
echo "path to me --------------->  ${0}     "
echo "parent path -------------->  ${0%/*}  "
echo "my name ------------------>  ${0##*/} "
echo
exit

# ------------- Test script------------- #

$  
/home/myscript/my_setting.sh
"'My name is'" "'Ryan'"
# ------------- RESULTS ------------- # arguments called with ---> 'My name is' 'Ryan' $1 ----------------------> 'My name is' $2 ----------------------> 'Ryan' path to me --------------> /home/myscript/my_setting.sh parent path -------------> /home/myscript my name -----------------> my_setting.sh