LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-18-2007, 12:01 AM   #1
amdGTintel
LQ Newbie
 
Registered: Dec 2006
Posts: 5

Rep: Reputation: 0
bash scripting : printing variable with the number within $i


hello.
I got this problem :
I want to print variable $1 to $n but for is not good enough in its current way, atleast how I use it :

for i in $(seq -w 1 9); do echo $$i; done
$i should give a number, but $$ is being done before so it's not good. I want it firstly to check what $i is and then do $ on the $i, if i=1 then $ $i = $1


How can I solve that?
\

Thanks!
 
Old 01-18-2007, 01:27 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Use indirection.
Code:
#!/bin/bash
for ((n=1;n<5;n++)); do
echo "${!n}"
done
./test.sh 12 43 56 78 90
12
43
56
78

Shift is usually used to handle one argument at a time.

Last edited by jschiwal; 01-18-2007 at 01:50 AM.
 
Old 01-18-2007, 01:30 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I find this is nice and easy:
Code:
#!/bin/bash

for arg in "$@"; do
    echo "we have an arg: $arg"
done
Use "$@" instead of $* to prevent problems with args which have whitespace in them.
 
  


Reply


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 Scripting - printing select lines in a file bullfrog1870 Linux - Newbie 16 11-08-2006 09:33 AM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 05:13 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
C programming: Variable number of parameters to function kenneho Programming 8 03-21-2006 06:21 AM
shell scripting: variable problem XST1 Programming 7 01-30-2005 01:45 PM


All times are GMT -5. The time now is 09:11 PM.

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