I have the following script (to get my current IP from an external service):
#!/bin/bash
####################################################################
# Gets the public IP address of current server
####################################################################
cmd='curl -s'
#cmd='wget -q -O'
#cmd='lynx -dump'
ipservice=checkip.dyndns.org
pipecmd="sed -e 's/.*Current IP Address: //' -e 's/<.*\$//'"
# Run command
echo $($cmd $ipservice | $pipecmd)
But sed command complains:
sed: -e expression #1, char 1: unknown command: `''
I have been googling around on how to use single quotes inside a variable without success.
Thanks!
The command is split into words
sed,-e,'s/.*Current,IP,Address:,//'etc., so the first command in thesedprogram indeed starts with', which is not a validsedcommand. Use an array and quoting instead:Note that
echo "$(command)"is equivalent tocommand. In general, make sure that you always quote all variables (there are a few exceptions, though).