0

I have gone through tons of SO questions and google searches and cant find the answer to this one. I have a sed command that, when run in my bash script fails, but works fine from command line. I know it must be some wierdness with bash, but cant find what.

Script as follows :

#!/bin/bash
SEARCH=$1
REPLACE=$2
FILE=$3
SED="sed -i 's~$SEARCH~$REPLACE~g' $FILE"
echo "Running $SED"
$SED

Have also tried instead of the last $SED line:

OUT=`$SED`
echo "Command output : $OUT"

but both versions give the following output when called with

# vmSub 'DOMAIN' 'example' mysql.sql
Running sed -i 's~DOMAIN~example~g' mysql.sql
sed: -e expression #1, char 1: unknown command: `''
Command output : 

But copy/paste the echoed command works :

# sed -i 's~DOMAIN~example~g' mysql.sql
#

It doesn't matter if the search string is in the file or not. Have tried using '/' as the separator in the sed command, same result. As I am just copy/pasting the command as echoed out by the script, then I know what I test via command line SHOULD match what is executed by the bash script. Have also ensured that none of the options passed to the script contain special characters or spaces, so i know its not that causing the problem.

Am running on Linux RHEL with GNU bash, version 4.2.46(2)-release and sed (GNU sed) 4.2.2 have also confirmed that my interactive shell is /bin/bash

SOLVED : As per @thatotherguy in comments below, the answer was to move the bash vars outside the quotes, so the code now reads :

SED="sed -i 's~"$SEARCH"~"$REPLACE"~g' "$FILE

This now seams to work.

Thanks everyone for your help.

Marl
  • 169
  • 1
  • 7
  • You need to use double quotes. Variables aren't expanded inside single quotes. – Barmar Oct 29 '19 at 17:37
  • Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Oct 29 '19 at 17:38
  • 1
    Hi, the bash string IS in dbl quotes, as shown by the fact the echo command shows the correct output, the single quotes are inside the dbl ones. If it was jsut a case of bash not expanding vars inside single quotes, then the echo command would output something like `Running sed -i 's~$SEARCH~$REPLACE~g' mysql.sql` wouldn't it? – Marl Oct 29 '19 at 17:40
  • 1
    @Marl You're right. This is because you're trying to put a shell command in a variable, so your quotes are literal instead of syntactical. This is similar to how in PHP, `$var="2+2"; print($var);` will not print `4` even though replacing the variable by hand will. I updated the duplicate – that other guy Oct 29 '19 at 17:42
  • See [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) Simplest solution: don't put commands in variables. Your current solution -- moving the variables outside quotes -- can fail depending on the content of the variables. – Gordon Davisson Oct 29 '19 at 20:29

0 Answers0