I want to write a script 'test.sh' that will take a user's input and replace all special characters with a '\' + the character. My script:
#!/bin/bash
echo 'input='"$1"
arg=`echo "$1" | sed 's:[]\[\^\$\.\*\/]:\\&:g'`
echo 'modified input='"$arg"
My command works on the string 'xy', which does not have any special characters. I run this in my terminal:
test.sh xy
And I obtain:
input=xy
modified input=xy
However, when I run this:
test.sh x.y
I get:
input=x.y
modified input=x&y
I don't understand why this script is not working. I expect to get:
input=x.y
modified input=x\.y
I think the problem lies in this sed command but I'm not sure where:
sed 's:[]\[\^\$\.\*\/]:\\&:g'