Why does ; after & lead to unexpected token error in bash on Linux?

The command using ‘;’ after ‘&’ like

ssh host1 hostname &; ssh host2 hostname &

Leads to error like

bash: syntax error near unexpected token `;'

Why does ; after & lead to unexpected token error in bash on Linux? And what’s the solution?

The fix

The quick fix is to change your command to

ssh host1 hostname & ssh host2 hostname &

The reason

“;” and “&” are command terminators (newline(s) imply “;” unless there is a “” to continue the line) and Bash does not allow empty commands.

&; means start the previous command in background (&) and the start a ; command in foreground. The later one is empty and is not allowed by Bash.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *