Can anyone tell me how can I make the follow command to work properly?
SERVER=192.168.1.1
ping $SERVER (It Works)
ping '$SERVER' (It doesn't work)
I want this to compose a more complex command that needs quotes!
Thank you all!!
Can anyone tell me how can I make the follow command to work properly?
SERVER=192.168.1.1
ping $SERVER (It Works)
ping '$SERVER' (It doesn't work)
I want this to compose a more complex command that needs quotes!
Thank you all!!
Use double quotes.
When you use single quotes, you get exactly what you typed, whilst double quotes interpolate, per the example below:-
$ x=1
$ echo 'This is $x'
This is $x
$ echo "This is $x"
This is 1
#ssh -t $SRV1 'ping $SRV2'
The remote SSH command execution requires single quotes. If I use #ssh -t $SRV1 'ping 192.168.1.2' it works, but I need use a variable for SRV2.
Thank you!
– Kellyson Feb 21 '18 at 20:21ssh -t "$SRV1" ping "$SRV2"
. BTW, if you need the remote host to see the double-quote (e.g. when the ssh command uses a remote variable), use \
to escape the double-quote inside the quoted command. e.g. ssh "$SRV1" "echo \$HOSTNAME: pinging $SRV2; ping $SRV2"
– cas
Feb 22 '18 at 00:43
You can use double-quotes like so:
SERVER=192.168.1
ping "$SERVER.1"
ping "$SERVER".1
ping $SERVER.1
Notice the last example has no quotes.
However, see Expansion of variable inside single quotes in a command in Bash for a much more in-depth reading on the topic.