[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bash: A Question

From: Lord Sam
Subject: Bash: A Question
Date: Wed, 3 Jun 2020 07:49:33 +0200


Note: All code is bolded, for readability.

The matter in question refers to the "Command Substitution" section of the
"Variables and Expansions" Chapter ( In particular, of the two
example commands comparing the *`(...)`* and the *$(...)* syntax—

*echo "`echo \"\`echo \\"hello\\"\`\"`"*


*echo "$(echo "$(echo "hello")")"*

—the former can be made, upon slightly adjusting the command, to produce
outputs that I could not find an explanation for, either by trying to
answer myself or by searching online.

The above commands, as implied by the text, are supposed to be equivalent.
And they do produce the same output:

*hello *

However, comparing the two implies that, in some way, both



*"hello" *

are equivalent.

To see why *\\"hello\\"* was used in the text, I decided to replace it with
*"hello"*. The resulting command:

*echo "`echo \"\`echo "hello"\`\"`"*

to my surprise, also outputted:


In fact,

*echo "`echo \"\`echo "hello"\`\"`"* (0 backslashes around *"hello"*)

*echo "`echo \"\`echo \"hello\"\`\"`"* (1 backslash around *"hello"*)

*echo "`echo \"\`echo \\"hello\\"\`\"`"* (2 backslashes around *"hello"*)

*echo "`echo \"\`echo \\\"hello\\\"\`\"`"* (3 backslashes around *"hello"*)

all yielded:


Note: When I say 'x backslashes around *"hello"*, I mean x backlashes on
both sides of *"hello"*

Further testing revealed the following revealed the following:

4, 5, 6, and 7 backslashes around *"hello"* outputted *"hello"*

8, 9, 10, and 11 backslashes around *"hello"* outputted *\hello\*

12, 13, 14, and 15 backslashes around *"hello"* outputted *\"hello\"*

16, 17, 18, and 19 backslashes around *"hello"* outputted *\\hello\\*

20 backslashes around *"hello"* outputted *\\"hello\\"*

It would appear that, for every four backslashes added around *"hello"* (that
is, four on each side), the output alternates between adding *""* around
the output expression (and keeping the amount of *\* in the output the
same), or adding an additional *\* to the output expression (and removing
the *""* around the output).

Why does this occur?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]