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:
The simpler, modern syntax:
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:
Now the syntax of yore:
Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)