LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2007, 09:29 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
how to nest commands


hi,

in a simple script if a user wants to print the first argument entered as a param when running script, the script can be simply as follows:

Code:
#!/bin/sh
echo "arg1: "$1

$./myscript hello
$arg1: hello

however, lets say in my script i have to use a variable, instead of the number '1', so it would look like:

Code:
#!/bin/sh
x=1
echo "arg1: "$x

$./myscript hello
$arg1: 1
the problem here is that this will print on the console:

because $x evaluates to 1. my question is, how do i tell the script to first evaluate the $x to 1 and then do a $1?

i know this has to do with nesting the command, so that an inner command gets evaluated and the resulting value gets passed to the outer command and i've tried several approaches like $($x), ${$x}, $`$x`, $$x, but none of them works. any guidance as where i am going wrong will be helpful. thanks!

Last edited by new_2_unix; 11-16-2007 at 09:32 PM.
 
Old 11-16-2007, 09:48 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
Depending which version of bash you're using, an indirect reference will do the job:
Code:
#!/bin/sh
x=1
echo "arg1: "$1
echo "Indirect reference: "${!x}
 
Old 11-16-2007, 10:20 PM   #3
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
yes, that did it!! thanks!
 
  


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
LXer: Open source Thunderbird e-mail client looks for new nest LXer Syndicated Linux News 0 07-28-2007 11:31 AM
LXer: Mozilla is pushing Thunderbird out of the nest LXer Syndicated Linux News 0 07-27-2007 07:01 AM
can you nest backticks? BrianK Programming 7 03-22-2007 12:40 PM
I am getting an error trying to nest Dynebolic, And a graphic anomaly McBidgeyFuggalo Linux - Newbie 0 09-03-2004 12:30 AM
flex c++ code about nest file?? chuanyung Programming 0 01-20-2004 03:36 AM


All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration