Printing all arguments passed to a bash script - Help

This is a discussion on Printing all arguments passed to a bash script - Help ; How do I iterate through the arguments, possibly containing spaces, passed to a bash script?...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Printing all arguments passed to a bash script

  1. Printing all arguments passed to a bash script

    How do I iterate through the arguments, possibly containing spaces, passed to a bash script?

  2. Re: Printing all arguments passed to a bash script

    "Angel Tsankov" writes:

    >How do I iterate through the arguments, possibly containing spaces, passed to a bash script?


    $1 $2 $3....
    shift
    man bash


  3. Re: Printing all arguments passed to a bash script

    On 2006-09-07, Angel Tsankov wrote:

    > 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 |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  4. Re: Printing all arguments passed to a bash script

    "Angel Tsankov" addressed the congregation
    thusly:

    > 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.

+ Reply to Thread