sed is not working in a bash script when a variable is used in the same copycat

An interesting fact about sed is, characters other than ‘/’ can be used as the separator.

sedArg="'s|userDir:""$currentPath""|userDir:""$newPath""'|"

Your code would result in

's|userDir:abc|userDir:xyz'|

The first thing to note is that the last quote and the separator are the wrong way round. But you don’t want to add single quotes to the variable. The error can be recreated like this:

echo "userDir:abc" | sed "'s|userDir:abc|userDir:xyz|'"

If you remove the single quotes it will work.

sedArg="s|userDir:""$currentPath""|userDir:""$newPath""|"

The extra quotes are unnecessary and can also be removed:

sedArg="s|userDir:$currentPath|userDir:$newPath|"

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top