Currently, only the script's arguments are passed as positional parameters. For compatibility reasons, $0 cannot be used to also pass the script's filename, so I'm creating a new BASH_SCRIPT variable instead.
Below is a heavily simplified test case. The goal is to stop it from asking who you are. Documentation changes included. Please Reply-To-All. Thanks. Marc. -- In an empty directory, first run ... #! /bin/sh rm -fr BASH_ENV bash_env cat > BASH_ENV <<"HERE" { echo -------------------------------------------------- echo In ${BASH_SOURCE[0]}... echo $0 "$@" set echo -------------------------------------------------- } | tee /dev/fd/2 | grep -q bash_env || echo 'Who are you?' HERE ln -f BASH_ENV bash_env exit 0 ... then, in the same directory, run (case matters) ... BASH_ENV=BASH_ENV $where_your_test_bash_is ./bash_env -- --- bash-5.1/doc/bash.1 +++ devel-5.1/doc/bash.1 @@ -1610,6 +1610,11 @@ matching the entire regular expression. The element with index \fIn\fP is the portion of the string matching the \fIn\fPth parenthesized subexpression. .TP +.B BASH_SCRIPT +The name of the script currently being executed by \fBbash\fP, as specified on +the command line. +This is set before any startup files are executed for the script. +.TP .B BASH_SOURCE An array variable whose members are the source filenames where the corresponding shell function names in the --- bash-5.1/doc/bashref.texi +++ devel-5.1/doc/bashref.texi @@ -5810,6 +5810,11 @@ matching the entire regular expression. The element with index @var{n} is the portion of the string matching the @var{n}th parenthesized subexpression. +@item BASH_SCRIPT +The name of the script currently being executed by bash, as specified on +the command line. This is set before any startup files are executed for +the script. + @item BASH_SOURCE An array variable whose members are the source filenames where the corresponding shell function names in the @code{FUNCNAME} array --- bash-5.1/shell.c +++ devel-5.1/shell.c @@ -686,6 +686,7 @@ main (argc, argv, env) else if (arg_index != argc && read_from_stdin == 0) { shell_script_filename = argv[arg_index++]; + bind_variable ("BASH_SCRIPT", shell_script_filename, 0); arg_index = bind_args (argv, arg_index, argc, 1); /* $1 ... $n */ } else