I have a script, like follows:
echo 'Hello world' # etc
function fun
if test 1 -gt 0
echo "$argv[1]"
end
end
# lots of code in between
fun
I call this file with ./file foo --bar
. I want fun
to print --bar
. Instead, it prints nothing, because the argv containing --bar
(ie, the file's argv) is being masked by the function's argv, which isn't receiving any input.
I could, theoretically, pass argv to the function, as in fun $argv
. However, this is very obnoxious to have to include with every single function call (I make extensive references to argv in this file). Is there a way to access the file-scope argv
variable from within functions in the file?
The outer argv is shadowed by the inner argv - a global variable shadowed by a local variable. There is no way to change this.
There are numerous ways around it, including what I would recommend: Use set -g fileargv $argv
once before you execute the functions and then use $fileargv
in your functions.