Differences between revisions 1 and 18 (spanning 17 versions)
Revision 1 as of 2007-05-02 23:12:30
Size: 1376
Editor: redondos
Comment:
Revision 18 as of 2013-12-29 11:52:15
Size: 365
Editor: ArielleN7
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[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"
}}}

Although direct access to any positional argument is possible this way, it's hardly needed. The common way is to use {{{getopts(3)}}} 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 here: http://www.shelldorado.com/goodcoding/cmdargs.html
Hello from United States. I'm glad to came across you. My first name is Damien. <<BR>>
I live in a city called Sumner in south United States.<<BR>>
I was also born in Sumner 32 years ago. Married in November 2004. I'm working at the post office.<<BR>>
<<BR>>
Also visit my blog ... [[https://underwaterfilters.shutterfly.com/|Under Sink Water Filter Reviews]]

Hello from United States. I'm glad to came across you. My first name is Damien.
I live in a city called Sumner in south United States.
I was also born in Sumner 32 years ago. Married in November 2004. I'm working at the post office.

Also visit my blog ... Under Sink Water Filter Reviews

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