One can connect to a wifi network through wpa_supplicant's command line interface, wpa_cli. wpa_cli has 2 modes of operation : interactive, and normal. In normal mode, one must pass the desired command through wpa_cli's arguments.

For exemple, to connect to a network, one must execute the following commands (as root) :

wpa_cli add_network
> 0 # this is the new network's ID
wpa_cli set_network 0 ssid '"SSID_HERE"'
wpa_cli set_network 0 psk '"PASSPHRASE_HERE"'
wpa_cli enable_network 0

All of these operations can be scripted. But wpa_cli requires, for set_network ssid and set_network psk, that both the SSID and passphrase are escaped by a single quote and a double quote, like this : '"ssid"'.

However, this escape format is hard to respect in a script when you want to replace the ssid by a variable's value, and wpa_cli keeps returning a FAIL exit status, instead of OK. The following patterns have been tried :

ssid '"$ssid"' # doesn't work, sets SSID to "$ssid" instead of $ssid's value
ssid \'"$ssid"\' # doesn't work

Hence, my question is : How to put the ssid in a variable in a script, and use it as a parameter to give wpa_cli a SSID/passphrase ?

  • ssid "'$ssid'" should work.
    – jimmij
    Feb 17, 2019 at 16:56
  • Nope, wpa_cli returns FAIL too. This is very weird, though, because this should be equivalent to my solution...
    – cocosushi
    Feb 17, 2019 at 17:47
  • Oh, just noticed I made a typo - the working pattern is '"' $ssid '"' instead of "'" $ssid "'"... Will edit my answer
    – cocosushi
    Feb 17, 2019 at 17:48

1 Answer 1


The following pattern is working for me, but I do not know if it is secure :

wpa_cli set_network 0 ssid '"'$ssid'"'

I guess only the ' ' are kept, along with $ssid's value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .