Differences between revisions 10 and 22 (spanning 12 versions)
Revision 10 as of 2008-11-18 12:58:54
Size: 339
Editor: ipd080
Comment: Good morning to all of you... Excellent web site I will be visiting oftene. pugster charmes 70 cake I like this site. http://lake13when-as.tripod.com/recipe47d.html supersaturated sodium acetate recip
Revision 22 as of 2014-01-27 14:13:22
Size: 2336
Editor: host-2-99-128-84
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Good morning to all of you... Excellent web site I will be visiting oftene. pugster charmes 70 cake I like this site. http://lake13when-as.tripod.com/recipe47d.html supersaturated sodium acetate recipe ring, http://fairisle58.free.bg/recipe18a.html outback and blue cheese chopped salad, With respect admiration. <<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:

{{{
    # Bourne
    for last
    do
        : # nothing
    done

    echo "last argument is: $last"
}}}
To get an argument by number, we can use a counter:

{{{
    # Bourne
    n=12 # This is the number of the argument we are interested in
    i=1
    for arg
    do
        if test $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"
}}}
and can be put into a helpful function:

{{{
    getarg() { # $1 is argno
        shift $1 && echo "$1"
    }
    arg12=$(getarg 12 "$@")
}}}
or to avoid the cost of a subshell, you can name the variable it should be written to:

{{{
    getargto() { # $1 is variable, $2 is argno
        local __="$1" && shift
        shift $1 && printf -v "$__" "$1"
    }
    getargto arg12 12 "$@"
}}}
In addition, bash and ksh93 treat the set of positional parameters as an [[BashFAQ/005|array]], and you may use [[BashFAQ/073|parameter expansion]] syntax to address those elements in a variety of ways:

{{{
    # Bash, ksh93
    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 seldom needed. The common alternative is to use {{{getopts}}} to process options (e.g. "-l", or "-o filename"), and then use either {{{for}}} or {{{while}}} to process all the remaining arguments in turn. An explanation of how to process command line arguments is available in [[BashFAQ/035|FAQ #35]], and another is found at http://www.shelldorado.com/goodcoding/cmdargs.html
Line 3: Line 66:
CategoryHomepage CategoryShell

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:

    # Bourne
    for last
    do
        : # nothing
    done

    echo "last argument is: $last"

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

    # Bourne
    n=12        # This is the number of the argument we are interested in
    i=1
    for arg
    do
        if test $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"

and can be put into a helpful function:

    getarg() { # $1 is argno
        shift $1 && echo "$1"
    }
    arg12=$(getarg 12 "$@")

or to avoid the cost of a subshell, you can name the variable it should be written to:

    getargto() { # $1 is variable, $2 is argno
        local __="$1" && shift
        shift $1 && printf -v "$__" "$1"
    }
    getargto arg12 12 "$@"

In addition, bash and ksh93 treat the set of positional parameters as an array, and you may use parameter expansion syntax to address those elements in a variety of ways:

    # Bash, ksh93
    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 seldom needed. The common alternative is to use getopts to process options (e.g. "-l", or "-o filename"), and then use either for or while to process all the remaining arguments in turn. An explanation of how to process command line arguments is available in FAQ #35, and another is found at http://www.shelldorado.com/goodcoding/cmdargs.html


CategoryShell

BashFAQ/025 (last edited 2024-03-28 14:56:51 by emanuele6)