Differences between revisions 3 and 4
Revision 3 as of 2007-08-30 01:57:33
Size: 1828
Editor: GreyCat
Comment: add another example and some cross-references
Revision 4 as of 2007-11-30 02:58:45
Size: 1852
Editor: GreyCat
Comment: change internal links
Deletions are marked like this. Additions are marked like this.
Line 38: Line 38:
In addition, [:BASH:] treats the set of positional parameters as an [#faq5 array], and you may use [#faq73 parameter expansion] syntax to address those elements in a variety of ways: In addition, [:BASH:] treats the set of positional parameters as an [:BashFAQ#faq5:array], and you may use [:BashFAQ#faq73:parameter expansion] syntax to address those elements in a variety of ways:
Line 46: Line 46:
Although direct access to any positional argument is possible this way, it's hardly needed. The common way is to use {{{getopts}}} to process command line options (e.g. "-l", or "-o filename"), and then use either {{{for}}} or {{{while}}} to process all arguments in turn. An explanation of how to process command line arguments is available in [#faq35 FAQ #35], and another is found at http://www.shelldorado.com/goodcoding/cmdargs.html Although direct access to any positional argument is possible this way, it's hardly needed. The common way is to use {{{getopts}}} to process command line options (e.g. "-l", or "-o filename"), and then use either {{{for}}} or {{{while}}} to process all arguments in turn. An explanation of how to process command line arguments is available in [:BashFAQ#faq35:FAQ #35], and another is found at http://www.shelldorado.com/goodcoding/cmdargs.html

Anchor(faq25)

How can I access positional parameters after $9?

Use ${10} instead of $10. This works for ["BASH"] and KornShell, but not for older BourneShell implementations. Another way to access arbitrary positional parameters after $9 is to use for, e.g. to get the last parameter:

    for last
    do
        : # nothing
    done

    echo "last argument is: $last"

To get an argument by number, we can use a counter:

    n=12        # This is the number of the argument we are interested in
    i=1
    for arg
    do
        if [ $i -eq $n ]
        then
            argn=$arg
            break
        fi
        i=`expr $i + 1`
    done
    echo "argument number $n is: $argn"

This has the advantage of not "consuming" the arguments. If this is no problem, the shift command discards the first positional arguments:

    shift 11
    echo "the 12th argument is: $1"

In addition, [:BASH:] treats the set of positional parameters as an [:BashFAQ#faq5:array], and you may use [:BashFAQ#faq73:parameter expansion] syntax to address those elements in a variety of ways:

    for x in "${@:(-2)}"    # iterate over the last 2 parameters
    for y in "${@:2}"       # iterate over all parameters starting at $2
                            # which may be useful if we don't want to shift

Although direct access to any positional argument is possible this way, it's hardly needed. The common way is to use getopts to process command line options (e.g. "-l", or "-o filename"), and then use either for or while to process all arguments in turn. An explanation of how to process command line arguments is available in [:BashFAQ#faq35:FAQ #35], and another is found at http://www.shelldorado.com/goodcoding/cmdargs.html

BashFAQ/025 (last edited 2014-04-24 10:02:07 by pgas)