Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
How can one access the 'n'th word in the command line argument ie. let there be an integer stored in a variable called 'n'. I want to access the 'n'th ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16

    How to access the last word in the command line argument

    How can one access the 'n'th word in the command line argument
    ie. let there be an integer stored in a variable called 'n'.
    I want to access the 'n'th word in the command line argument
    Is there any way to do it??

    thanx in advance

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    You can call in a variable by using in a script $1, $2, $3...for passed variables 1,2 and 3 respectively.

    Is it that you want to check for the last variable passed or you know the specific count of the final variable/argument?

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16
    no i want to know how to access an arbitrary element in the command line arguments.
    For eg. Let us say that somehow out of the blues we get an integer which is between 1 & $# let this integer be stored in a variable called 'm'. The task is to access this 'm'th word in the command line.

  4. #4
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16
    I dont want to use loops for this purpose.

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    How about this:

    m=3

    tmp="wrd=\"\$$m\""
    eval $tmp
    echo "$wrd"

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    There is a long thread related to this at: Finding the last command line argument (bash) - LinuxQuestions.org

    The beginning title was "last argument", but there are many solutions to getting any argument you desire ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #7
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16
    Thanks a lot fellas for ur help.....
    i liked the solution of vsemaska....

  8. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If you decide to use that style, I suggest you generalize it by adding braces. Otherwise an expression beyond 9 becomes a problem:
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate value of braces.
    
    set -- a1x a2x a3x a4x a5x a6x a7x a8x a9x a10x a11x a12x a13x
    set -- one two three four five six seven eight nine ten eleven twelve thirteen
    
    m=3
    
    tmp="wrd=\"\$$m\""
    eval $tmp
    echo "$wrd"
    
    m=13
    
    tmp="wrd=\"\$$m\""
    eval $tmp
    echo "$wrd"
    
    tmp="wrd=\"\${$m}\""
    eval $tmp
    echo "$wrd"
    
    exit 0
    Which produces:
    Code:
    % ./s1
    three
    one3
    thirteen
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #9
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16

    How to get the 'n'th word in a line

    Let us assume that there is a line:
    line="He is a very good boy"
    Now I wanna get the 'n'th word of this line?
    ie. if the value of n=3 then we should get the 3rd word of this line, which is "a"
    in this case.

    And once again no loops please...
    thanx in advance.....

  10. #10
    Linux Newbie
    Join Date
    Jul 2004
    Posts
    143
    just assign the line to an array and you can retrieve the word whatever you want...

    array=(He is a very good boy)
    or
    line="He is a very good boy"
    array=($line)

    echo ${array[3]} --> very

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...