Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-01-2007, 12:25 AM   #1
LQ Newbie
Registered: Apr 2003
Distribution: Ubuntu 7.04, RedHat 4.4
Posts: 22

Rep: Reputation: 15
bash script arguments

I wrote a bash script that takes in an undefined number of arguments. As part of the script, I want to iterate through these variables. The problem is that I need the script to take the argument corresponding to the current iteration value. I tried "echo $$k" where $k is the iterator and received an answer equal to value stored in k, and tried "echo $"$k"" and received $ followed by variable stored in k, I also tried a bunch of other things that returned unsuccessful results, interestingly "echo $$$k" produces a large unexplainable number, a memory pointer???

So for example, say I have the following:
./script testing 123

#desired output: testing
echo $$k #output: 1
echo $"$k" #output: $1
echo \$"$k" #output: $1
I realize I could take $@ and cut it up into separate strings using space as divider, but I really want to know if my original way of doing it is possible? Thanks.
Old 11-01-2007, 01:49 AM   #2
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
Cool question. Here's what I found.

Doing a "shift" in the script will remove the first argument in the argument list until none are left.


echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
echo "Parameters: ${@}"
echo "P1 = $1"
So, you could technically just shift each time you go through and use $1 (if you don't care about accessing the args anymore). Or, do a for loop on $@.

But a direct answer to your question, I'm not sure.

Read a bit in here, maybe it'll say more...

Old 11-01-2007, 04:37 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
edit: this isn't what you mean is it?

you aren't being very clear, have you a simple example?

use for

# "$@" preserves spaces
for thing in "$@"; do
    echo $thing
$ hello there old 'fellow me lad'
fellow me lad

Last edited by bigearsbilly; 11-01-2007 at 04:39 AM.
Old 11-01-2007, 06:04 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Originally Posted by R3N3G4D3 View Post
but I really want to know if my original way of doing it is possible? Thanks.
Yes. An example is by means of the eval command. You want to interpret $1 where 1 itself is a variable? You simply do
eval echo \$$k
the first $ sign is escaped to let literal interpretation by eval, therefore the command executed is exactly
echo $1
for k equal to 1. Another way can be using arrays, if available in your version of bash. Just assign arguments to array's elements and retrieve them with the index k, as in
echo ${args[$k]}
In this case, just take in mind that array elements are zero-based.

Last edited by colucix; 11-01-2007 at 06:17 AM.
Old 11-01-2007, 10:40 AM   #5
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Yet another way ("YAW" ?), perhaps the easiest, is indirect expansion:
for X in "$@"
  echo ${!X}   # echos the value of the variable X
For more info, search the bash man page for "exclamation point".

BTW & FWIW, combining the 2 techniques will finally give me double indirection:
$ K=k1 ;  A=K ;  Z=A
$ eval echo \$${!Z}
Old 11-01-2007, 11:16 AM   #6
LQ Newbie
Registered: Apr 2003
Distribution: Ubuntu 7.04, RedHat 4.4
Posts: 22

Original Poster
Rep: Reputation: 15
Thanks for the replies guys, the "eval echo \$$k" command did exactly what I needed. I'm sure other solutions work too, but that is the easiest modification to my script.

Last edited by R3N3G4D3; 11-01-2007 at 11:18 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - variable arguments indikabandara19 Linux - Newbie 4 05-21-2007 11:40 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 09:03 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 06:23 PM
bash arguments stuckinhell Programming 6 08-13-2004 06:10 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration