Within your program
shell function, use "$@"
to refer to the list of all command line arguments given to the function. With the quotes, each command line argument given to program
would additionally be individually quoted (you generally want this).
program () {
"$GOBIN"/program "$@"
}
You would then call program
like so:
program -p hello_world -tSu
or, if you want to pass hello world
instead of hello_world
,
program -p 'hello world' -tSu
Using $1
refers to only the first command line argument (and $2
would refer to the second, etc.), as you have noticed. The value of $1
would additionally be split on white-spaces and each generated string would undergo filename globbing, since the expansion is unquoted. This would make it impossible to correctly pass an argument that contains spaces or filename globbing patterns to the function.
foo() { echo "1: $1"; echo "2: $2"; }; var="a b"; foo $var
, whereby bash "splits" the string invar
into two arguments tofoo
(namely, "a" and "b"). (Zsh does away with this legacy behaviour, so quotes are rarely needed.) – Zorawar Nov 24 '20 at 02:02foo() { ls $1; }
andfoo "-l foo.txt"
in Zsh, it wouldn't work exactly because of the lack of splitting, andfoo -l foo.txt
also wouldn't pass all the arguments along. Regardless of the shell, you still need$@
to pass all the arguments. – ilkkachu Nov 24 '20 at 08:48$@
joined the discussion, though. – Zorawar Nov 24 '20 at 15:51$1
include all the other arguments shows a certain lack of understanding of how and why quotes are needed in shell code. – Zorawar Nov 24 '20 at 17:38