views:

255

answers:

3

Either I missed some backlash or backlashing does not seem to work with too much programmer-quote-looping.

$ echo "hello1-`echo hello2-\`echo hello3-\`echo hello4\`\``"

hello1-hello2-hello3-echo hello4

Wanted

hello1-hello2-hello3-hello4-hello5-hello6-...
+5  A: 

It's a lot easier if you use bash's $(cmd) command substitution syntax, which is much more friendly to being nested:

$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
Mark Rushakoff
This is not restricted to *bash*. It is available in all shells that conform to POSIX 1003.1 (“POSIX shells”) and most Bourne-derived shell (*ksh*, *ash*, *dash*, *bash*, *zsh*, etc.) though not the actual Bourne shell (i.e. http://heirloom.sourceforge.net/sh.html ).
Chris Johnsen
+6  A: 

Use $(commands) instead:

$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4

$(commands) does the same thing as backticks, but you can nest them.

You may also be interested in Bash range expansions:

echo hello{1..10}
hello1 hello2 hello3 hello4 hello5 hello6 hello7 hello8 hello9 hello10
Joey Adams
+1 like the {1..10}. Limit it with array? ZSH can "${$( date )[2,4]}". Why not: "echo ${echo hello1-$(echo hello2)[1]}"?
HH
+4  A: 

if you insist to use backticks, following could be done

$ echo "hello1-`echo hello2-\`echo hello3-\\\`echo hello4\\\`\``"

you have to put backslashes, \\ \\\\ \\\\\\\\ by 2x and so on, its just very ugly, use $(commands) as other suggested.

S.Mark