Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the semicolon do when it is run in a bash command? [duplicate]

Tags:

bash

terminal

gnu

For example, when running

echo a; echo b

in the terminal, its output is:

a
b

It seems to me that the semicolon waits for the first command (echo a) to finish, then it starts the second command (echo b).

Q: Is the semicolon just used for iterating over commands in bash?

Q: What does the semicolon do when it is run in a bash command?

like image 784
Static Avatar asked Dec 20 '14 05:12

Static


People also ask

What does semicolon do in bash?

Semi-colons and newlines separate synchronous commands from each other. Use a semi-colon or a new line to end a command and begin a new one. The first command will be executed synchronously, which means that Bash will wait for it to end before running the next command.

What does adding a semicolon at the end of a command do?

The Semicolon lets the compiler know that it's reached the end of a command.

Does shell script need semicolon?

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 does dash d do in bash?

-d is a operator to test if the given directory exists or not. For example, I am having a only directory called /home/sureshkumar/test/. This condition is true only when the directory exists. In our example, the directory exists so this condition is true.


2 Answers

The ; separates the two commands.

echo a; echo b

It lets the bash know that echo a and echo b are two separate commands and need to be run separately one after the other

Try without semicolons

$ echo a echo b
a echo b

Here the statement is taken as a single command echo and a echo b is passed as parameter to the echo

like image 148
nu11p01n73R Avatar answered Nov 15 '22 06:11

nu11p01n73R


it's a way to simulate a newline.

echo a; echo b

is equivalent to

echo a

echo b

like image 31
Coding Orange Avatar answered Nov 15 '22 07:11

Coding Orange