And (&&) Operator in ShellScript

The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.


command1 && command2

command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1. and it returns true only and only if all commands execute successfully.

└─$ ping -c 1>/dev/null && echo "Internet working fine."
Internet working fine.


# os == linux && user == root
if [[ ${OS_TYPE} == "Linux" &&  ${UID} -eq 0 ]]
    echo "user is root user and os is linux."

let's execute the above program as a non-root user and see the output

└─$ ./

now let's run the same program as a root user.

└─$ sudo su
[sudo] password for gaurav:
└─# ./
user is root user and os is linux.

Demo Video

Click Here For Demo Video