I usually like to echo certain string which have an exclamation point in them. And while i know how to escape them to prevent expansion by the Shell, i noticed it also print the backslash...
echo "\!" # will print \! instead of just !
Contrary to other tool like sed which does not print the backslash if used to prevent expansion of possible valid parameter used by sed:
echo "test" | sed 's/^\(.*\)$/\1 \!/' # print -> test !
echo "test" | sed 's/^\(.*\)$/\1 !/' # also print -> test !
echo "test" | sed "s/^\(.*\)$/\1 ! /" # also print -> test !, though needed to put a space or use a backslash for it to show correctly
How can i do the same with echo or I'm i obliged to use other tools like sed?
EDIT:
Just noticed using '' instead of "" with the echo example above works for printing ! without shell expansion (both with and without the backslash), though:
- It wouldn't work in instance when one want to use command substitution with
echo, since it only work with""...
set +H). – ilkkachu Mar 25 '21 at 15:27echofor anything that may contain backslashes or may start with-. – Stéphane Chazelas Mar 25 '21 at 15:28echo, Thanks! @StéphaneChazelas – Nordine Lotfi Mar 25 '21 at 15:34