Difference between "&&" and ";"?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
mkfs.ext4 /dev/vg_thin/thin_vol_client1 && mkfs.ext4 /dev/vg_thin/thin_vol_client2 && mkdfs.ext4 /dev/vg_thin/thin_vol_client3
Say instead of "&&" I used ";" what difference would I see?
 


If you have looked into programming especially in C this could be confusing about &&.

In that language IINM there have to be at least two conditions which must both return 1, so the whole thing is 1 and so it's processed.

However in the "shell", the successful execution exit code is zero. If it returns 1 then the further commands after && are not executed. This is important to understand, that programming in "bash" shell language is not the same as in C about some logic operators.

The semicolon is "blind faith" and you have to be careful about where you use it. In fact, use && if there is a doubt about the semicolon, or execute commands one line at a time.
 
mkfs.ext4 /dev/vg_thin/thin_vol_client1 && mkfs.ext4 /dev/vg_thin/thin_vol_client2 && mkdfs.ext4 /dev/vg_thin/thin_vol_client3
Say instead of "&&" I used ";" what difference would I see?

With the "&&", the second mkfs.ext4 command will be executed only if the first succeeds and the third will be executed only if the second succeeds.

"&&" (logical AND) says you want -all- of the given commands to succeed. Once any one of them fails, they cannot "all succeed" so any remaining commands will be skipped entirely.

With ";" (semicolon) the commands are independent and each will be tried regardless of the others' success or failure.

Assuming that the command echo something will always succeed, and knowing that true will always succeed and false will always fail, note the results of the following:

Code:
$ echo -n A && true && echo -n B && true && echo -n C && true && echo -n D ; echo "."
ABCD.

$ echo -n A && true && echo -n B && true && echo -n C && false && echo -n D ; echo "."
ABC.
 
Well said, above ^^^

The double ampersand (&&) is referred to as

conditional

Wizard
 
While it's not really what the OP asked, we could go all TLDR about "||" and "!" as well, but maybe there's a more appropriate thread or sub-forum for that?
 
Command Line ... but we're in it, lol - you could always start a thread on operands and such like, just look around first to see what has been covered.

Wiz
 
For what it's worth, I wrote a generic article on this subject - and it's very basic, only covering the few I would expect most folks to use.


So, there's that. A deep dive into the specifics would be a good idea.
 
"||" (logical OR) says you want -any- of the given commands to succeed. Once any one of them succeeds, that condition is met so any remaining commands will be skipped entirely.

In the first line of the following example,
touch /tmp/poop
succeeded (because /tmp/ is writable by regular users) so there was no need to execute
echo "unable to touch /tmp/poop"
and nothing was echoed.

In the second line of the example,
touch /usr/poop
failed (because /usr/ is not writable by regular users, as evidenced by the "...Permission denied" message) so
echo "unable to touch /usr/poop"
-was- executed.

Code:
$ 
$ touch /tmp/poop || echo "unable to touch /tmp/poop"
$
$ touch /usr/poop || echo "unable to touch /usr/poop"
touch: /usr/poop: Permission denied
unable to touch /usr/poop
$

We can combine "&&" and "||" to create the effect of an if-then-else-fi" structure.

Code:
$ touch /tmp/poop && echo "OK" || echo "NOOOOO"
OK
$ touch /usr/poop && echo "OK" || echo "NOOOOO"
touch: /usr/poop: Permission denied
NOOOOO
$

However, in my opinion, if you get into any more complex logic, it's worthwhile (for visual clarity) to use the more verbose if-then-else-fi syntax and (also IMO) if you're going to do that, it might be better to write a multi-line script instead of trying to do it directly on the command line.
 
touch /tmp/poop

LOL You gotta learn about 'foo' and 'bar'. While it does come from FUBAR (fouled up beyond all recognition, depending on your desire to speak in polite company) it's the 'universal' example - outside of 'example' itself.

So, it's 'touch foo' and 'touch bar'...

Or stick with poop. I kinda prefer that you do, 'cause I'm pretty much just a giant five year old and it made me giggle.
 
Using "&&": When you use the "&&" operator between commands, it acts as a logical AND operator. It means that the second command will only execute if the first command succeeds (returns an exit status of 0). If the first command fails (returns a non-zero exit status), the second command will not execute.
Using ";": On the other hand, when you use the ";" operator between commands, it acts as a command separator. It means that the commands separated by ";" will execute sequentially, regardless of the exit status of each command. Each command will execute one after the other, regardless of whether the previous command succeeded or failed.
 
I once knew a geek who always used "fred" for junk file names because the letters are right there together (on a qwerty keyboard). I think he was left handed, but I'm right handed and the letters in "poop" are even more "right there together". As so often happens, a temporary file sometimes becomes "temporarily persistent" so I sometimes have plop, loki and juki floating around, too, so as not to overwrite (step on?) poop. It's all very complicated. ;)

Whenever I find one of these files that's more than a couple of years old, I can't resist having a look to see what was so important hat I didn't want to lose it around but yet so unimportant that it didn't warrant a proper file name - but it's usually just that I forgot to clean up after myself.

. . . later . . .

Daaannnggg... a quick "find" reveals:

poop.py from June 2020 - "hello world" in python

poop.ps1 from November 2020 - "hello world" in powershell (don't judge me - I wanted to learn powershell as I had no decent scripting language on my work PC. Hated it!)

poop.tgz from June 2015 - containing, if you can believe it, the contents of the "poop" directory - some software packages from whatever version of Tiny Core was current in 2015
 
pool? polk? loki? Was this "geek" called Wade but lied it was Fred? Lazy programmers LOL.

On one computer the "O" key was a bit too active, so "pool" over "polo" for that one, but never thought of it. (evil smile)

Those are true Python waste products right there.
 
@wendy-lebaron - There were -a lot- of "o"s in that post! His name was actually "John"


I'm thinking maybe an error log should be named opoo.

That occurred to me last night when I thought to myself <sanitized> Oh, poo! </sanitized> as I recorded video of the BDE learning how to open the very secure baby gate that confines her, or used to confine her, to the data center. The lil wench stopped working on it every time she saw me looking and acted all innocent ("nothin to see here - I'm just lookin for somethin t' bark at") every time she caught me watching but went back to it as soon as I looked away. It was thoroughly adorable. I had to hold the phone sideways, peeking past my elbow to get the video. :eek:
 


Latest posts

Top