Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why does an ampersand in URL cause failure of the following command?

I want to open a web page by running Safari in Terminal, placing the URL as an argument.

This works as expected:

open -a safari ""

But once the quotes being removed, it wouldn't work as expected:

open -a safari

Instead, it returns:

[3] 5395
[4] 5396
[1]   Done                    open -a safari
[2]   Done                    searchOption=all

Seems like ampersand in the URL was treated as "running previous command in the background", though there is no space following the ampersand in the URL.

share|improve this question
up vote 2 down vote accepted

The ampersand is a command delimiter (similar to the semicolon ;), but indicates that the command is to be run in the background.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed

Therefore you ran three commands:

  • open -a safari " in the background
  • searchOption=all in the background
  • query=good in the foreground

The latter two are simply variable assignments, so no error was displayed (as is often the case when this happens accidentally).

though there is no space following the ampersand in the URL.

This isn't needed for either & or ; to end a command:

$ ls;pwd
Applications Desktop      Documents    Downloads    Library      Movies       Music
Pictures     Projects     Public       Work
share|improve this answer

You must log in to answer this question.

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