Discussion:
Printing all arguments passed to a bash script
(too old to reply)
Angel Tsankov
2006-09-07 13:07:34 UTC
Permalink
How do I iterate through the arguments, possibly containing spaces, passed to a bash script?
Bill Marcum
2006-09-07 13:56:18 UTC
Permalink
["Followup-To:" header set to comp.os.linux.misc.]
On Thu, 7 Sep 2006 16:07:34 +0300, Angel Tsankov
Post by Angel Tsankov
How do I iterate through the arguments, possibly containing spaces,
passed to a bash script?
man bash, look for getopts

or

for arg; do
do_something_with "$arg"
done

or

while [ $# -gt 0 ]; do
do_something_with "$1"
shift
done
--
I used to work in a fire hydrant factory. You couldn't park anywhere near
the place.
-- Steven Wright
Unruh
2006-09-07 14:34:50 UTC
Permalink
Post by Angel Tsankov
How do I iterate through the arguments, possibly containing spaces, passed to a bash script?
$1 $2 $3....
shift
man bash
Chris F.A. Johnson
2006-09-08 00:26:20 UTC
Permalink
Post by Angel Tsankov
How do I iterate through the arguments, possibly containing spaces,
passed to a bash script?
for var
do
: do something with $var
done

Or:

for var in "$@"
do
: do something with $var
done


man bash:

Compound Commands
...
for name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in
turn, and list is executed each time. If the in word is omit-
ted, the for command executes list once for each positional
parameter that is set (see PARAMETERS below). The return status
is the exit status of the last command that executes. If the
expansion of the items following in results in an empty list, no
commands are executed, and the return status is 0.
...
Special Parameters
The shell treats several parameters specially. These parameters may
only be referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first
character of the IFS special variable. That is, "$*" is equiva-
lent to "$1c$2c...", where c is the first character of the value
of the IFS variable. If IFS is unset, the parameters are sepa-
rated by spaces. If IFS is null, the parameters are joined
without intervening separators.
@ 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" ... When there are no positional parameters, "$@" and $@
expand to nothing (i.e., they are removed).
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Preacher Kane
2006-09-08 20:45:01 UTC
Permalink
Post by Angel Tsankov
How do I iterate through the arguments, possibly containing spaces,
passed to a bash script?
You'll need a loop:
$* = all arguments.
$# = number of arguments.

If an argument contains spaces then it needs to be enclosed in quotes...
else the script won't know if it's two arguments or one containing a space.
Loading...