steventryintolearn
steventryintolearn

Reputation: 1

Different behaviour for these commands

I'm having trouble understanding why there is a difference in the outputs of these 3 commands when executed on different distributions Could you guys help me understand why ?

 printf "%s" `echo ../.. | sed 's/[.]/\\&/g'`
output on kali: &&/&&~    output on ubuntu: &&/&&

printf "%s" $(echo ../.. | sed 's/[.]/\\&/g')
output on kali: \.\./\.\.   output on ubuntu: ../..

printf "%s" "$(echo ../.. | sed 's/[.]/\\&/g')"
output on kali: \.\./\.\.     output on ubuntu: \.\./\.\.

Upvotes: 0

Views: 52

Answers (1)

KamilCuk
KamilCuk

Reputation: 141768

Kali uses Zsh shell. Ubuntu uses Bash shell.

output on ubuntu: &&/&&

Do not use backticks - prefer $(...). Backticks remove \ before execution.

printf "%s" `echo ../.. | sed 's/[.]/\\&/g'`

becomes:

printf "%s" $(echo ../.. | sed 's/[.]/\&/g')

So replaces . by & character.

output on kali: &&/&&~

The ~ on the end in Zsh in your configuration represents output from a command without a newline.

output on ...: \.\./\.\.

That should be understandable.

 output on ubuntu: ../..

Aaaand this is the hard one around Bash 5.0. Unquoted result of command substitution undergo filename expansion (also called pathname expansion). The \.\./\.\. matches the directory path ../... Bash 5.0 introduced a change that would make the result of expansion undergo pathname expansion even if it doesn't have any globbing special characters.

The change was reversed in Bash 5.1. Bash-5.1-alpha available for download https://lists.gnu.org/archive/html/bug-bash/2019-01/msg00063.html.

Upvotes: 3

Related Questions