Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$0 in sourced Bash script not returning script name

Tags:

People also ask

What is $0 return bash?

$0 expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file.

What is $0 shell?

The $0 special variable can be used in the terminal to print the name of your current shell simply by executing the following statement: $ echo $0.


When I reference $0 in a Bash script on my Mac running Mac OS 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 produced:

> . 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?