What is the difference between & and && in SHELL?

Asked

Viewed 7,210 times

3

I’m using the terminal of Ubuntu.

I learned a certain command to free the memory, which is :

free & sync

In another case, I learned something like

nohup &&

After all, what are these two operators called (& and &&) and what are the differences between them?

  • Correct me if necessary! I don’t know the difference between terminal, shell and bash

  • Only one reference for learning, which I am using and may be useful, https://www.codecademy.com/pt/courses/learn-the-command-line

  • 2

    It would be nice to take a look at this answer: http://answall.com/questions/72969/cmd-console-ms-dos-e-relatedthermos-related

1 answer

7


The operator & (Ampersand) is used to put the previous command on background and, if there is a later command, it will be executed independent of the result of the previous command:

free & sync

When this operator is used at the end of a command on a Linux terminal, it means that you want to run that command and already release the terminal for new commands, without waiting for the result of the last command:

wget http://answall.com &

The operator && (AND) is used so that the next command will only be executed if the previous command has been successfully executed (exist status equal to 1):

mkdir pasta && cd pasta

More details you can find here.

  • 1

    Very enlightening. Thank you, friend!

  • Output 0 means it has been successfully executed. Any other value means error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.