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
}
There is a
;
needed at the end of the function:should work.
The syntax of a function definition for bash is specified as
Note that it includes a
;
that is not part of thelist
.That the
;
is required in this place is kind of a syntax anomaly. It is notbash
specific, it's the same forksh
, but it the;
is not required inzsh
.Functions in
bash
are essentially named compound commands (or code blocks). Fromman bash
: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.The end of a single command (";") is implied by the newline. In the oneline version
}
is parsed as an argument to the unterminatedls -a
command. Which you can see if you do:See how the command inside the function declaration swallows the trailing curly brace?