Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: How to call each argument in a while loop

  1. #11
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,333
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to call each argument in a while loop

    Code:
     i=1; while (( i<=$# )); do echo "\$$i: ${!i}"; ((i++)); done
    Code:
    $ ./arg a bb ccc dddd
    $1: a
    $2: bb
    $3: ccc
    $4: dddd
    or
    Code:
    while (( $# )); do echo "$1"; shift; done;
    this one is destructive (consumes params)
    Last edited by Vaphell; March 11th, 2013 at 01:26 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  2. #12
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: How to call each argument in a while loop

    Quote Originally Posted by toad3000 View Post
    because I tried many ways to accomplish this using a while loop and It wasn't working. I made a counter and I tried ${$counter} but I get bad substitutional error.
    Oh, I see. You are trying to use indirect expansion. As you can see from the example posted by Vaphell, the format of indirect expansion is, ${!name}:
    Code:
    name=1
    echo "\$$name is: ${!name}"
    For more details please check out BashFAQ 006 (link in my signature) and the `Parameter Expansion' section from the man page of bash:
    Code:
    man bash | less '+/^ +Parameter Expansion'

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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