Use a variable that could contain special characters into sed?

I have a bash script that'll take any word passed to it as a parameter and then encrypt the word using an openssl command. From there I want to take the returned string and use sed to write it into another file. The encrypted value sometimes contains the "/" character (but not always) at which point sed fails to function.

Here's the gist of what the script looks like:

#!/bin/bash
fileDirectory"/etc/ci"
fileName="key.bin"
encryptedOSPFPW='echo $1 | openssl enc -a -A -aes-256-cbc -md sha256 -pass file:"$fileDirectory"/"$fileName"'
sed -i "s/OSPFPW/$encryptedOSPFPW/g" /etc/quagga/ospfd.conf
exit 0

How can I adjust the sed command so that it'll substitute whatever is returned into literally. I know that if I replace the double-quotes with single-quotes, sed will take whatever's supplied to it literally, but that doesn't work when I'm using a variable.

Thanks

7

1 Answer

The answer to this question was to use a different delimiter with the sed command. I ended up using the following command:

sed -i "s|OSPFPW|$encryptedOSPFPW|g" /etc/quagga/ospfd.conf
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like