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?
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.
The Semicolon lets the compiler know that it's reached the end of a command.
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”.
-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.
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
it's a way to simulate a newline.
echo a; echo b
is equivalent to
echo a
echo b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With