The positional parameters of the caller's scope are not available in a function. You'd need the caller to pass them to the function one way or another.
In bash
, that could be done with an array (but beware that arrays other than "$@"
in bash
start at indice 0 instead of 1 (like in ksh
, but contrary to all other shells)).
f() {
printf 'Caller $1: %s\n' "${caller_argv[0]}"
printf ' My $1: %s\n' "$1"
}
caller_argv=("$@")
f blah
Or pass them in addition:
f() {
printf 'Caller $1: %s\n' "$2"
shift "$(($1 + 1))"
printf 'My $1: %s\n' "$1"
}
f "$#" "$@" blah
Here with $1
containing the number of positional arguments of the caller, so f
knows where its own args start.