Which exact one-line conditional construct activates an existing Internet connection if it's off and deactivate it if it's on?

I looked some on-line pages to understand the use of

  1. the "nmcli" command
  2. the "if then else fi" construct
  3. command substition, i.e. the use of "$(command_name)" function to get the output of a command
  4. the "grep" command to manage the nmcli command output

The final construct should be of this kind:

if ( nmcli MyConnection status = down ); then nmcli c up id "MyConnection"; else nmcli c down id "MyConnection"; fi

Note: the if condition is not real code and should be elaborated.

But I didn't find an option for the nmcli command to test if a connection is up or down. I thought of using the "status" option, i.e.: "nmcli c status id "MyConnection"", but while nmcli gives a grep-parsable output message when "MyConnection" is up, it returns an error and doesn't exit to the command line when "MyConnection" is down (the cursor in the terminal window stays there and a CTRL+C is needed to get the shell prompt back).

I need some help to arrange these things into a complete one-line script usable in the

System Settings > Keyboard > Shortcuts > Custom Shortcuts


Thank you very much.

P.S.: This question follows what learned here some time ago: How to set a keyboard shortcut to activate an existing Internet connection?

SO is Ubuntu 13.04, Internet connection type is Mobile broadband


This should do it.

if [[ -z $(nmcli c status id MyConnection | grep -E 'STATE\:\s+activated') ]]; then nmcli c up id MyConnection; else nmcli c down id MyConnection; fi

If you're going to use it from a non-bash environment, you're going to need to wrap the command in bash like so:

bash -c 'if [[ -z $(nmcli c status id MyConnection | grep -E 'STATE\:\s+activated') ]]; then nmcli c up id MyConnection; else nmcli c down id MyConnection; fi'
  • Thank you very much Oli, it works in the terminal window. It does not work when set as a shortcut key combination though. It seems to be something linked with the conditional statement, since other combined shortcuts like "/usr/bin/python /usr/bin/software-center" do work. I'll store the script into a file for now, though it's not ideal. Thank you also for the rfkill info. NOTE for people who set a language that's not English in the system preferences: the "STATE\:\:s+activated" text must be changed to match the text written in the "nmcli c status id ..." command output. – Andrew Aug 5 '13 at 14:44
  • @Andrew When you run it from some places, it's not going to use bash to run it (rather a standard exec environment) and we need bash here. This is fixed by wrapping it. I've edited the answer to show this. – Oli Aug 5 '13 at 14:48
  • Thank you very much for your help Oli. I changed single quotes to double quotes around "STATE\:\s+activated" since it was in conflict with single quotes around the whole command passed to bash. Now it works fine. Maybe your post should be updated. Thank you very much. – Andrew Aug 5 '13 at 15:07
  • Just be careful with double-quotes as in some places (in bash, for example) the $() segment will parse before the rest of it. That shouldn't hurt it in this situation but as I say, be careful. – Oli Aug 5 '13 at 15:09
  • I changed them because the script didn't work: it started the connection in both cases, when it was down and when it was up too: it didn't turn it down. I thought it was a problem with the single quotes and looked for an escape character but then I got it working with the double quotes. Maybe there's a better solution. – Andrew Aug 5 '13 at 15:15

You should have a look at the Advanced Bash Scripting Guide:


I will not explain the details of the script, you should figure that out yourself as an learning exercise.

#!/usr/bin/env bash
if [ -z $1 ]

nmcli c status id $essid 2>&1 > /dev/null

if [ $? -eq 0 ]

nmcli c $action id $essid
  • Thank you very much, mgor. For what concerns the script specifically, I'll use Oli's one, since it's on a single row. – Andrew Aug 5 '13 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.