How to connect to proper network

I use multiple networks and for my printer i wanted to create a macro to change my network if necessary. Found the shell script for this:
networksetup -setairportnetwork en0 FRITZ!Box Gerrit password

The problem is the space in my ssid. Must i change this or is there another simple solution

When working with shell scripts and something that has spaces, you can usually do 1 of 3 things.

  1. Put 'single quotes' around the words: 'My Network'

  2. Put "double quotes" around the words: "My Network"

  3. Put a \ before the space: My\ Network

Personally, I dislike option 3 but it's valid nevertheless.

A few more suggestions / tips / best practices:

  1. Use 'single quotes' if the thing you are quoting includes "double quotes": e.g. 'My "Guest" Network'

  2. Use double quotes if the thing you are quoting includes 'single quotes: e.g. "Gerrit's Network"

  3. If you need to expand a variable such as $MY_NETWORK_NAME you must use "double quotes" to get the variable to be expanded, e.g. do not use '$MY_NETWORK_NAME' use "$MY_NETWORK_NAME"

  4. If you are quoting something which has Special Characters in it and you want to make sure they are NOT evaluated as special characters (such as $ or ! but also others), then you should use 'single quotes' e.g. 'A#JK@$$kaAF# BI@GSD ! ASDHDS'

In light of #4, my recommendation is this: if there is not a single quote in the thing you are quoting and you do not explicitly need to expand variables, it is more recommended to use 'single quotes' instead of "double quotes" although either will work in most cases.

Using single quotes can save you from some accidental "Oops, the shell thought that $ was referring to a variable but I really just wanted a literal $ there!" situations.

Let me know if any of this was unclear. It is not always easy to recognize what others who are new to this may have questions about.

Hi @tjluoma,
thanks for the detailed explanation. Single quotes made it work.

1 Like