1

I searched the forum as there are already a lot of similar posts, but I cannot find an answer to my problem.

I have an issue with using sed inside the bash script. I set one of the variables to the value I'm passing as an argument to sed, but I get this error:

sed: -e expression #1, char 1: unknown command: `''

this is what is inside my bash script that is relevant to this part:

currentPath="\"/home/user/SYNC_A\",";
newPath="\"/home/user/SYNC_B\",";
sedArg="'s|userDir:""$currentPath""|userDir:""$newPath""'|"
sed -i "$sedArg" /home/user/settings.js

this is what I see when I use echo $sedArg

's|userDir:"/home/user/SYNC_A",|userDir:"/home/user/SYNC_B",|' /home/user/settings.js

When I copy and paste this into the command line it works fine, also when I copy what is printed by echo into the bash script it is working fine. Anybody is able to explain this to me in lame terms so I can find down what I'm doing wrong?

K_Dec
  • 13
  • 3
  • what happens if you remove the single quotes when you are assiging `sedArg` ? – Sundeep Nov 04 '20 at 15:39
  • personally, I'd remove `sedArg` altogether and construct the `sed -i` command with single-quote/double-quote strings side by side.. see https://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables for example and other gotchas – Sundeep Nov 04 '20 at 15:41
  • @Sundeep do you mean use `sed -i $sedArg /home/user/settings.js` ? I tried and it didn't work. The second suggestion didn't work either. – K_Dec Nov 04 '20 at 15:59
  • use double quotes around your sed expression instead of single quote. – Sudosu0 Nov 04 '20 at 16:20

1 Answers1

1

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|"
DavidW
  • 323
  • 1
  • 8
  • sed will return `no input files ` – K_Dec Nov 04 '20 at 16:09
  • The input file is /home/user/settings.js – DavidW Nov 04 '20 at 16:34
  • you are right about the single quote being in the wrong place, this will cause an `unterminated command` error. I made this mistake when I was preparing the post, it's not present in the original script. I just tried your proposal on my laptop and it's working. I will test it with my script tomorrow. I always thought that I have to pass the arguments encapsulated to sed otherwise it will not work. Thank you for your help. – K_Dec Nov 04 '20 at 18:05
  • It will be just enough to say: `sedArg="s|userDir:$currentPath|userDir:$newPath|"`. – tshiono Nov 04 '20 at 23:55
  • 1
    @tshiono That's right - I have edited my answer to show this. – DavidW Nov 05 '20 at 08:57