Two substitutions in one echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two substitutions in one echo
# 1  
Old 09-19-2011
Two substitutions in one echo

Code:
PHOST1=temp
i=1

I want to display the value of PHOST1 by making use of variable i inplace of 1


something like this
Code:
echo "$PHOST$i"  # -> This doesn't seem to work.

Please provide me the correct syntax. I tried many different ways
Code:
echo ${PHOST${i}}
echo ${PHOST[$i}}

Nothing seems to be working. Someone please suggest.

Last edited by Scott; 09-19-2011 at 03:30 AM.. Reason: Code tags
# 2  
Old 09-19-2011
Welcome to the forum.

Code:
eval PHOST${i}=temp

or you are looking for this?

Code:
eval echo \$PHOST${i}

# 3  
Old 09-19-2011
Thanks for the response, but I have a small issue here.

This is how the script looks like
Code:
PHOST1=temp
i=1
for ((i=1;i<2;i++))
do
        echo "The hostname is `eval echo \$PHOST${i}`"
done

The desired output should be

"The hostname is temp". But it shows "The hostname is 1"

Please help me resolve-

Last edited by Scott; 09-19-2011 at 03:31 AM.. Reason: Code tags
# 4  
Old 09-19-2011
Try...
Code:
$ PHOST1=temp
$ for ((i=1;i<2;i++)); do eval $(echo echo "The hostname is \$PHOST${i}"); done
The hostname is temp

# 5  
Old 09-19-2011
Great, That works fine.

A small question, how do we make use of \t in echo command with -e flag.

I tried giving

Code:
eval $(echo echo -e " The hostname is \t \$PHOST${i}") #   -> This doesn't seem to work.

Moderator's Comments:
Mod Comment Please use code tags
# 6  
Old 09-19-2011
Quote:
Originally Posted by blazer789
Great, That works fine.

A small question, how do we make use of \t in echo command with -e flag.

I tried giving

eval $(echo echo -e " The hostname is \t \$PHOST${i}") -> This doesn't seem to work.

Use $(...) in place of `...` in the first solution.
"-e" should also work in that.

Code:
PHOST1=temp
for ((i=1;i<2;i++))
do
        echo -e "The hostname is \t $(eval echo \$PHOST${i})"
done


Quote:
The desired output should be

"The hostname is temp". But it shows "The hostname is 1"
If using traditional `...`, Double escape the statement.

Code:
echo "The hostname is `eval echo \\$PHOST${i}`"

I would suggest to use $(...) form.
# 7  
Old 09-20-2011
Thanks for the inputs. Very helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speeding up substitutions

Hi all, I have a lookup table from which I am looking up values (from col1) and replacing them by corresponding values (from col2) in another file. lookup file a,b c,d So just replace a by b, and replace c by d. mainfile a,fvvgeggsegg,dvs a,fgeggefddddddddddg... (7 Replies)
Discussion started by: senhia83
7 Replies

2. UNIX for Dummies Questions & Answers

Multiple substitutions in one expression using sed

Hi, I'm trying to get multiple substitutions in one expression using sed: echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s//-/g" So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash) I have provided bad example. The question is how to use multiple... (6 Replies)
Discussion started by: useretail
6 Replies

3. Shell Programming and Scripting

How can I write nested command substitutions?

Hello How can write the nested command substitutions? echo `expr substr $x 1 expr ${#x} - 1` the above code is not working! Thanks in advance Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

4. Shell Programming and Scripting

arrays and substitutions

I am working on a bash script and ran around this issue. here's the code : #!/bin/bash string="\"bin\" \"barn\" \"bin, barn /\"" array=($string) echo -e "\nMethod 1\narray is ---> ${array}" echo -e "array=($string)" array=("bin" "barn" "bin, barn /") echo -e "\nMethod 2\narray is... (4 Replies)
Discussion started by: titou_dude
4 Replies

5. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

6. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

7. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies

8. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

9. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

10. UNIX for Dummies Questions & Answers

String substitutions in ASCII files -

We need to scramble data in a number of ASCII files. Some of these files are extremely large (1.2 GB). By scrambling, I mean that we need to substitute certain strings, which number around 400, with scrambled strings. An example has been given below If "London" occurs in the file, then it... (2 Replies)
Discussion started by: SanjivNagraj
2 Replies
Login or Register to Ask a Question