Using quotes on a sudo encapsulated sed command already using quotes

471 views Asked by At

I have a shell script, where I want to change the following text in a JSON file:

"foo-bar": true

to this:

"foo-bar": false

I want to do this by using the sed command. However, the JSON script resides in my /etc directory. Therefore, I have to encapsulate the whole command in a sudo sh -c command.

Here was my first attempt:

sudo sh -c "sed -e 's/"foo-bar": true/"foo-bar": false/' /etc/sample.json > /etc/sample2.json"

I thought the : was causing a problem so I escaped them, but that didn't help:

sudo sh -c "sed -e 's/"foo-bar"\: true/"foo-bar"\: false/' /etc/sample.json > /etc/sample2.json"

Still didn't work. So, I tried the double-quotes which someone suggested on a similar post on Stackoverflow:

sudo sh -c "sed -e 's/""foo-bar""\: true/""foo-bar""\: false/' /etc/sample.json > /etc/sample2.json"

But to no avail.

Now, I'm really stumped. Because it is an encapsulated sudo command, I need the extra quotes around the entire command and I believe this is what is causing the problem. But how can I get around it?

1

There are 1 answers

0
Oldest Software Guy On

Try doing this:

echo sed -e 's/"foo-bar": true/"foo-bar": false' /etc/sample/sjon >/etc/sample2.json' | sudo sh

In other words, build the command locally then let sh(1) pick up the command from stdin.