I am creating a script for my router (running busybox).
In my script I generate a command in a variable like nvram set option='value with spaces'
but when it is executed it complains that arguments are invalid. If I echo the same command and copy/paste it into the terminal it works fine.
#!/bin/sh
client="client1"
option="desc"
value="Client number 4"
cmd="nvram set vpn_${client}_${option}='${value}'"
echo $cmd;
echo $cmd
;
When running this script, it outputs the following:
nvram set vpn_client1_desc='Client number 4'
usage: nvram [get name] [set name=value] [unset name] [show] [commit] [save] [restore] [erase] [fb_save file]
usage: nvram [save_ap file] [save_rp_2g file] [save_rp_5g file] [save_rp_5g2 file]
The first line is my echo of the command and the two next are output from the nvram set command which complains about the arguments. It works if spaces in the value are omitted.
What am I missing here?
PS. My router is an ASUS RT-AC87U running latest version of Merlin firmware.
cmd() { nvram set vpn_${client}_${option}='${value}'; }
and then runvar=$(cmd); echo "$var"
. (echo "$(cmd)"
is useless, you could just runcmd
directly instead of capturing the output and printing that out in two steps.) – ilkkachu Apr 17 '21 at 11:22nvram set vpn_${client}_${option}="$value"
directly. It appears that single quotes were an issue so double quotes solved it. – ulvesked Apr 17 '21 at 11:56nvram set vpn_${client}_${option}='$value'
, the variable$value
isn't expanded. That's the difference between double and single quotes, one expands, the other doesn't. (Then again, if you didcmd="nvram set vpn_${client}_${option}="${value}""
, that would be completely different again, the result wouldn't have any quotes within it, since the second"
terminates the first and so for the two others.) https://mywiki.wooledge.org/Quotes and https://unix.stackexchange.com/questions/503013/what-is-the-difference-between-and-quotes/503014#503014 – ilkkachu Apr 17 '21 at 13:19