Semicolons superfluous at the end of a line in shell scripts?
BashShellSyntaxBash Problem Overview
I have a shell script which contains the following:
case $1 in
0 )
echo $1 = 0;
OUTPUT=3;;
1 )
echo $1 = 1;
OUTPUT=4;;
2 )
echo $1 = 2;
OUTPUT=4;;
esac
HID=$2;
BUNCH=16;
LR=.008;
Are semicolons completely superfluous in the snippet above? And is there any reason for some people using double semicolons?
It appears semicolons are only a separator, something you would use instead of a new line.
Bash Solutions
Solution 1 - Bash
Single semicolons at the end of a line are superfluous, since the newline is also a command separator. case
specifically needs double semicolons at the end of the last command in each pattern block; see help case
for details.
Solution 2 - Bash
According to man bash
:
> metacharacter
> A character that, when unquoted, separates words. One of the following:
> | & ; ( ) < > space tab
> control operator
> A token that performs a control function. It is one of the following symbols:
> || & && ; ;; ( ) | |&
So, the ;
can be metacharacter or control operator, while the ;;
is always a control operator (in case command).
In your particular code, all ;
at the end of line are not needed. The ;;
is needed however.
Solution 3 - Bash
In the special case of find, ;
is used to terminate commands invoked by -exec. See the answer of @kenorb to this question.
Solution 4 - Bash
@Opensourcebook-Amit
newlines equivalent to single semicolon ;
on terminal or in shell script.
See the below examples:
On terminal:
[root@server test]# ls;pwd;
On shell script:
[root@server test]# cat test4.sh
echo "Current UserName:"
whoami
echo -e "\nCurrent Date:";date;
[root@server test]#
But I am not agree with the comment that &
is equivalent to newline or single semicolon
&
is run commands in background also a command separator but not worked as semicolon or newline.