Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  37] [ 0]  / answers: 1 / hits: 23057  / 3 Years ago, thu, july 1, 2021, 12:10:52

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
}

More From » scripts

 Answers
0

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.


[#23274] Friday, July 2, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hical

Total Points: 498
Total Questions: 106
Total Answers: 117

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;