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?
Try doing this:
In other words, build the command locally then let sh(1) pick up the command from stdin.