Lurch
Lurch

Reputation: 875

echo quotes in bash script

I'm creating an automatic network configuration script and in it i have

#!/bin/bash
sudo rm /etc/default/ifplugd  
sudo echo "INTERFACES=""
HOTPLUG_INTERFACES="wlan0 eth0"
ARGS="-q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"" > /etc/default/ifplugd

however on viewing /etc/default/ifplugd some of the quotes are missing

INTERFACES=
HOTPLUG_INTERFACES=wlan0 eth0
ARGS=-q -f -u0 -d10 -w -I
SUSPEND_ACTION=stop

How do I configure the script so it includes the quotes between the first and last echo ones?

Upvotes: 0

Views: 482

Answers (3)

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

A heredoc provides an elegant solution:

sudo tee << EOF /etc/default/ifplugd
INTERFACES=""
HOTPLUG_INTERFACES="wlan0 eth0"
ARGS="-q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"
EOF 

This way, you don't have to manually quote each and every "" around, and you are not removing the ifplugd file, so you won't need to reset permissions after creating it.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246807

How about:

sudo sh -c 'cat <<END >/etc/default/ifplugd
INTERFACES=""
HOTPLUG_INTERFACES="wlan0 eth0"
ARGS="-q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"
END
'

You don't need to explicitly rm, the > redirection will truncate the file before writing the new content.

Upvotes: 3

madebydavid
madebydavid

Reputation: 6517

You need to escape the " marks with a \ prefix, like this:

#!/bin/bash
sudo rm /etc/default/ifplugd  
sudo echo "INTERFACES=\"\"
HOTPLUG_INTERFACES=\"wlan0 eth0\"
ARGS=\"-q -f -u0 -d10 -w -I\"
SUSPEND_ACTION=\"stop\"" > /etc/default/ifplugd

Upvotes: 2

Related Questions