Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly nest Bash backticks

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-... 
like image 726
hhh Avatar asked Apr 17 '10 02:04

hhh


People also ask

What is backtick in bash?

The Open Group has a definition for the backtick operator, officially referred to as command substitution. This operator is not the single quote character, but another character known as the grave accent ( ` ).

What does the C flag do in bash?

The manual page for Bash (e.g. man bash ) says that the -c option executes the commands from a string; i.e. everything inside the quotes.

What does $() do bash?

$() means: "first evaluate this, and then evaluate the rest of the line". On the other hand ${} expands a variable. I'm afraid the answer is just that the bash syntax for while just isn't the same as the syntax for for .

What does Backquote mean in shell?

The back quote is the one to use when you want to assign output from system commands to variables. It tells the shell to take whatever is between the back quotes as a system command and execute its output. Using these methods you can then substitute the output into a variable.


2 Answers

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 
like image 128
Joey Adams Avatar answered Oct 20 '22 21:10

Joey Adams


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.

like image 43
YOU Avatar answered Oct 20 '22 20:10

YOU