Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolons superfluous at the end of a line in shell scripts? [duplicate]

Tags:

syntax

bash

shell

People also ask

Do shell scripts need semicolons?

A semicolon or ampersand ( ; or & ) in a shell script is a command terminator. You can't use it if it doesn't follow a command. ; means “run the preceding command in the foreground” and & means “run the preceding command in the background”.

What is double semicolon in shell script?

The double semicolon is also useful as it leaves no ambiguity in the code. It is required as it is used at the end of each clause as required by the bash syntax in order to parse the command correctly. It is only used in case constructs to indicate that the end of an alternative.

What does semicolon mean in shell?

When the shell sees a semicolon (;) on a command line, it's treated as a command separator -- basically like pressing the ENTER key to execute a command. When would you want to use a semicolon instead of pressing ENTER? It's nice when you want to execute a series of commands, typing them all at once at a single prompt.

How do you end a line in shell script?

How to Get a New Line in Shell Script. Newline is used to identify the end of a line in a script to mark the beginning of a new line, referred to as line break or line feed. Just as you will put the br tag in HTML scripts to force the subsequent characters into a new line, that's what newline means in shell scripts.


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.


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:
         || & && ; ;; ( ) | |& <newline>

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.


In the special case of find, ; is used to terminate commands invoked by -exec. See the answer of @kenorb to this question.


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