If the arguments are to be stored in a script variable and the arguments are expected to contain spaces, I wholeheartedly recommend employing a "$*" trick with the input field separator set to tab IFS=$'\t' [Wayback].
Some bash parameter propagation links that hopefully will work with ash/dash too
Posted by jpluimers on 2021/10/27
For my link archive; I started with [Wayback] dash get all parameters quoted – Google Search:
- [Wayback] Handling positional parameters [Bash Hackers Wiki] has this handy table:
Parameter(s) Description $0the first positional parameter, equivalent to argv[0]in C, see the first argument$FUNCNAMEthe function name (attention: inside a function, $0is still the$0of the shell, not the function name)$1 … $9the argument list elements from 1 to 9 ${10} … ${N}the argument list elements beyond 9 (note the parameter expansion syntax!) $*all positional parameters except $0, see mass usage$@all positional parameters except $0, see mass usage$#the number of arguments, not counting $0These positional parameters reflect exactly what was given to the script when it was called.
Option-switch parsing (e.g.
-hfor displaying help) is not performed at this point.See also the dictionary entry for “parameter”.
- [Wayback] Handling positional parameters: mass usage [Bash Hackers Wiki] has this summary table:
Syntax Effective result $*$1 $2 $3 … ${N}$@$1 $2 $3 … ${N}"$*""$1c$2c$3c…c${N}""$@""$1" "$2" "$3" … "${N}" - [Wayback] Parameter expansion [Bash Hackers Wiki]
- The elaborate answer to [Wayback] How to pass all arguments passed to my bash script to a function of mine? – Stack Overflow by [Wayback] User Gordon Davisson – Stack Overflow of which the first part is:
The
$@variable expands to all command-line parameters separated by spaces. Here is an example.abc "$@"When using
$@, you should (almost) always put it in double-quotes to avoid misparsing of arguments containing spaces or wildcards (see below). This works for multiple arguments. It is also portable to all POSIX-compliant shells.It is also worth nothing that
$0(generally the script’s name or path) is not in$@.The Bash Reference Manual Special Parameters Section says that
$@expands to the positional parameters starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is"$@"is equivalent to"$1" "$2" "$3".... - Last part of the [Wayback] Accessing bash command line args $@ vs $* – Stack Overflow answer by [Wayback] Serge Stroobrandt:
and comment by Serge as well:
Here is [Wayback] an example, which includes quoted input. The input also matters!
- [Wayback] Propagate all arguments in a bash shell script – Stack Overflow (thanks [Wayback] Sdaz MacSkibbons for the answer and [Wayback] Ilan Schemoul to make it understandable by applying proper naming of the script files):
Use
"$@"instead of plain$@if you actually wish your parameters to be passed the same.Observe:
$ cat no_quotes.sh #!/bin/bash echo_args.sh $@ $ cat quotes.sh #!/bin/bash echo_args.sh "$@" $ cat echo_args.sh #!/bin/bash echo Received: $1 echo Received: $2 echo Received: $3 echo Received: $4 $ ./no_quotes.sh first second Received: first Received: second Received: Received: $ ./no_quotes.sh "one quoted arg" Received: one Received: quoted Received: arg Received: $ ./quotes.sh first second Received: first Received: second Received: Received: $ ./quotes.sh "one quoted arg" Received: one quoted arg Received: Received: Received: - a
–jeroen






Leave a comment