Johngoldenboy
Johngoldenboy

Reputation: 168

BASH: syntax error near unexpected token `done' with alias named 'do'

this question seems frequently asked but still there is no satisfying answer regarding my issue. Hence, I was hoping you could help me out.

The following error occurs opening a terminal under Ubuntu LTE 13.04:

syntax error near unexpected token done' bash: /usr/share/bash-completion/bash_completion: line 225: done'

I checked the specific 'bash_completion' - seems fine. Furthermore, I narrowed it down to the following command in my .bashrc file.

alias do='rsync -r -e ssh --exclude='file.py' [email protected]:/path/to/folder /do/here'

Though the following works just perfectly without raising any exceptions:

alias up='rsync -r -e ssh --exclude='file.py' /path/to/folder [email protected]:/do/here'

I checked if it does occur because of the --exclude flag, but it doesn't. Seems like something is wrong with the command. Though both commands just perfectly do their job. Only the first raises the error. Any ideas ?

Upvotes: 4

Views: 2490

Answers (3)

fangorn
fangorn

Reputation: 31

As I also said in another thread, by mistake I aliased the local word, which also gave the same corrupted behavior of the bash_completion scripts... So, just a kind reminder to avoid aliasing forbidden words like local do done etc... Hope it helps :) Cheers

Upvotes: 0

Mr. Llama
Mr. Llama

Reputation: 20909

In bash, do is a reserved word. It's looking for a do ... done block. Just name your alias something else.

Upvotes: 11

Charles Duffy
Charles Duffy

Reputation: 295679

do is a shell keyword. You can't use it as an alias name without causing bugs.

Beyond that -- consider using functions rather than aliases; they provide substantially more control. This alias might be rewritten as so:

do_sync() {
  rsync -r -e ssh --exclude='file.py' \
    [email protected]:/path/to/folder \
    /do/here \
    "$@"
}

Using functions instead of aliases lets you do conditional logic, insert arguments in a position other than the tail, and significantly simplifies quoting (which, for your alias here, almost certainly wasn't doing what you wanted it to, since you were using the same quote type both around the alias text and within the alias text with no escaping).

Upvotes: 4

Related Questions