Ubuntu – Write function in one line into ~/.bashrc

bashrcscriptssyntax

Why when I try to write a function just in one line into .bashrc file,

list(){ ls -a }

I get error?

bash: /home/username/.bashrc: line num: syntax error: unexpected end of file

but when I write it in multi line it's ok?

list(){
    ls -a
}

Best Answer

Functions in bash are essentially named compound commands (or code blocks). From man bash:

Compound Commands
   A compound command is one of the following:
   ...
   { list; }
          list  is simply executed in the current shell environment.  list
          must be terminated with a newline or semicolon.  This  is  known
          as  a  group  command. 

...
Shell Function Definitions
   A shell function is an object that is called like a simple command  and
   executes  a  compound  command with a new set of positional parameters.
   ... [C]ommand is usually a list of commands between { and },  but
   may  be  any command listed under Compound Commands above.

There's no reason given, it's just the syntax.

Since the list in the one-line function given isn't terminated with a newline or a ;, bash complains.

Related Question