Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

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 ?

like image 257
Johngoldenboy Avatar asked Dec 06 '22 02:12

Johngoldenboy


2 Answers

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

like image 136
Mr. Llama Avatar answered Feb 01 '23 12:02

Mr. Llama


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).

like image 38
Charles Duffy Avatar answered Feb 01 '23 12:02

Charles Duffy