Welcome to the most active Linux Forum on the web.
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 12-28-2011, 01:26 PM   #1
LQ Newbie
Registered: Jan 2006
Location: China
Distribution: Fedora
Posts: 28

Rep: Reputation: 0
Smile How to make cascading 2 variables valid?


I want to make below define valid in my script.


for (( i = 1; 1 < 3; i++ ))
echo "Search $F{$i} string ......"
grep -r $F{$i} /etc/

But I cannot get the expected result $F1 and $F2 in 1st and 2nd time.

How I can make it?


Old 12-28-2011, 01:52 PM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
It would be easier to use arrays.
F=("123456" "abcdef")
for ((i = 0; i<2; i++)); do
   echo "Searching ${F[$i]} string ..."
   grep "${F[$i]}" /etc/*
However doing it your way, you need to use `eval' and escape the `$' sign.
eval grep -r \$F$i /etc/

To see how the arguments are evaluated, and passed on to a command, replace the command with `set' and then echo $*.
set grep -r \$F$i /etc/
echo $*
grep -r $F1 /etc/

Last edited by jschiwal; 12-28-2011 at 02:15 PM.
Old 12-28-2011, 02:40 PM   #3
LQ Newbie
Registered: Jan 2006
Location: China
Distribution: Fedora
Posts: 28

Original Poster
Rep: Reputation: 0

Hi jschiwal,

So nice to get it work. I like array method.

For eval, I updated my script as below but it couldn't work as array method did.

for (( i = 1; 1 < 3; i++ ))

echo "Search \$F$i string ......"
eval grep -r \$F$i /etc/



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
how to make permanent changes to environment variables? cgchayan Programming 8 09-30-2009 03:57 PM
GNU Make - Variables as Targets Matthew Applegate Programming 11 08-09-2009 09:28 AM
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 05:32 AM
How to make script cmd valid as execut it directly? cdcshu Linux - Software 5 06-05-2007 06:58 AM
How can you make valid mdk9.2 rpms from the source? kvtournh Mandriva 1 11-18-2003 12:12 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration