• Uncategorized

About linux : Different-behaviour-for-these-commands

Question Detail

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: \.\./\.\.

Question Answer

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'`


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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.