Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script sed usage causing issues in ZSH

Tags:

bash

shell

sed

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?

like image 995
davetgreen Avatar asked Jul 09 '26 10:07

davetgreen


1 Answers

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"
like image 102
heemayl Avatar answered Jul 12 '26 00:07

heemayl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!