$ 0 in source Bash script does not return script name

When I reference $ 0 in a Bash script on my Mac running Mac X v10.6.7 (Snow Leopard), I get -bash, not the name of the script.

I ran the script described in this stack overflow question :

#!/bin/bash

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

The following is issued:

> . show_parms.sh foo

# arguments called with ($@) -->  foo
# $1 -------------------------->  foo
# $2 -------------------------->
# path to me ($0) ------------->  -bash
# parent path (${0%/*}) ------->  -bash
# my name (${0##*/}) ---------->  -bash

Any ideas?

+5
source share
3 answers

This makes sense since you are using a script.

. show_parms.sh foo

instead of doing it

./show_parms.sh foo

As explained in this answer to the same question , you can use $BASH_SOURCEto find out the name of the file from which the commands were received.

+8
source

$0 . . , script bash ; "- bash", . script :

chmod +x show_parms.sh
./show_params.sh foo

, . OS X; bash ( sh).

+3

This is because you are using. operator. If you just typed

show_parms.sh foo

You will get the desired result.

+1
source

All Articles