How can I write nested command substitutions?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I write nested command substitutions?
# 1  
Old 05-03-2013
How can I write nested command substitutions?

Hello

How can write the nested command substitutions?

Code:
echo `expr substr $x 1 expr ${#x} - 1`

the above code is not working!

Thanks in advance

Regards
Chetanz
# 2  
Old 05-03-2013
It should not be necessary to use "expr", ever.

Code:
$ echo ${x:1:${#x}-1}

(assuming your intention of extracting a substring of x; and those indexes are zero-based)
# 3  
Old 05-03-2013
Should you actually ever need nested command substitutions, they're trivial if you use the modern syntax. Unless you are forced to use an ancient shell which does not support $(cmd), never use `cmd`.

One level of nesting using the deprecated, paleo-backtick command substitution syntax:
Code:
$ echo top `echo 0 \`echo 1\``
top 0 1

The simpler, modern syntax:
Code:
$ echo top $(echo 0 $(echo 1))
top 0 1

It may not seem like a big difference, but there are other subtleties involved. The backtick syntax has a more complicated and intrusive quoting mechanism whereby the top-level interferes with the code of nested subshells. If you don't know how it works, it will cause you pain. If, however, you do know how it works, it will also cause you pain.

With the modern syntax, anything in a substitution is written just as it is outside of a substitution, regardless of how deeply it's nested.

A contrived, comparitive demonstration of three levels of nesting. First, the modern syntax:
Code:
$ echo top $(echo 0 $(echo 1 $(echo 2 $(echo 3))))
top 0 1 2 3

Now the syntax of yore:
Code:
$ echo top `echo 0 \`echo 1 \\\`echo 2 \\\\\\\`echo 3\\\\\\\`\\\`\``
top 0 1 2 3

Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
# 4  
Old 05-03-2013
Good post. I missed the subject title completely!
# 5  
Old 05-03-2013
Code:
echo `expr substr $x 1 expr ${#x} - 1`

At least one reason your nested substitution does not work is because you only have one set of `` backticks. Regardless of whether you use the older backtick or newer $() syntax, nested substitution won't work if only one substitution. You would need another set of backticks around the internal expr command.

An easy, I would suggest better, alternative is "don't nest". Do one substitution, store the results in an intermediate variable. Use the intermediate variable in the next substitution.

It's simpler to understand, self-documenting, and easier to troubleshoot. The negative is it takes an extra line. A very small price to pay.
This User Gave Thanks to hanson44 For This Post:
# 6  
Old 05-03-2013
Quote:
Originally Posted by hanson44
An easy, I would suggest better, alternative is "don't nest". Do one substitution, store the results in an intermediate variable. Use the intermediate variable in the next substitution.

It's simpler to understand, self-documenting, and easier to troubleshoot. The negative is it takes an extra line. A very small price to pay.
Excellent advice.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The Shell lost the inverted comma in a nested ssh command

Hi, i want use this Comand for my psql request sh ssh -o StrictHostKeyChecking=no rootatemailaddress.de sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'" but the shell lost the inverted comma for datnam=\'$DB\'. The request deliver... (2 Replies)
Discussion started by: peterpane007
2 Replies

2. Shell Programming and Scripting

Nested case: rsync command not found

Hello! Below my first bash script. As you can see i build 2 nested cases. The second one (sync databases) is working fine. Bu the first one (sync datadirs) is not working. It says: rsync: command not found. However when i move the rsync command to the top of the script its working. So i suppose... (2 Replies)
Discussion started by: hyperconnected
2 Replies

3. 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

4. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

5. Shell Programming and Scripting

Two substitutions in one echo

PHOST1=temp i=1 I want to display the value of PHOST1 by making use of variable i inplace of 1 something like this echo "$PHOST$i" # -> This doesn't seem to work. Please provide me the correct syntax. I tried many different ways echo ${PHOST${i}} echo ${PHOST Nothing seems... (6 Replies)
Discussion started by: blazer789
6 Replies

6. 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

7. 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

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

Nested Echo Command Help

I am doing below : $HOST_NAME1="webisstg70" $count=1 $echo $HOST_NAME1 $count webisstg70 1 $HOST_NAME="" $HOST_NAME=`echo '$HOST_NAME'${count}` $echo $HOST_NAME HOST_NAME1 $NODE_NAME=`echo $`echo ${HOST_NAME}`` I get below error message (2 Replies)
Discussion started by: findprakash
2 Replies
Login or Register to Ask a Question