Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 15
how to nest commands


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:

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:

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 10:32 PM.
Old 11-16-2007, 10:48 PM   #2
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

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

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


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

All times are GMT -5. The time now is 03:17 AM.

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
Open Source Consulting | Domain Registration