-2

I am passing a value to a .sh file as an argument (the file contains follows).

ESCAPED_REPLACE=$(printf '%s\n' "$1" | sed -e 's+$+\\$+g; s+(+\\(+g; s+"+\\"+g')
echo $ESCAPED_REPLACE

I basically want to replace every $ with \$ and every ( with \(.

If I pass Ver$ify

./code.sh "Ver$ify"

I am getting Ver\$ printed which should be Ver\$ify does anyone know the reason for this?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Kavin404
  • 959
  • 2
  • 11
  • 18
  • See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus May 24 '21 at 07:25

1 Answers1

1

Research the difference between single and double quotes, see https://shellcheck.net, re-read a good introduction to shell scripting.

You do not pass Ver$ify, you are passing Ver with the result of ify variable. Use single quotes.

./code.sh 'Ver$ify'

I basically want to replace every $ with \$ and every ( with \(

sed 's/[$(]/\\&/g'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111