davetgreen davetgreen - 3 months ago 12
Bash Question

Bash script sed usage causing issues in ZSH

I have a Bash script that I use to pull in several repos to create a new instance of our project workflow, which carries out a number of string replacements based on information provided from the command line.

A colleague uses ZSH and has been experiencing issues that seem to be as a result of the use of

sed
in the script. Specifically it seems to be that it's not processing the regex? For example...

# Author Name.
if [[ $authorname ]]
then
sed -i "" "s/Author Name/$authorname/g" "$file"
fi


Resulting in the following error:

sed: can't read "s/Author Name/$authorname/g" : No such file or directory


We've found that by adding the
-e
flag the majority of the string replacement errors go away, however he still gets a number of 'not found' errors...

sed: can't read : No such file or directory


Is there a better way to carry out the string replacement that is both Bash and ZSH friendly?

Answer

Presumable you are using GNU sed, which does not take -i "" like patterns for editing the file in place like BSD sed.

From man sed:

-i[SUFFIX], --in-place[=SUFFIX]

You need to remove the space in between:

sed -i"" "s/Author Name/$authorname/g" "$file"

Or as you are not taking any backup, simply do:

sed -i "s/Author Name/$authorname/g" "$file"
Comments