If statement

We can use the true command even in the if statement. This mechanism is illustrated in the below script.  

Example:

#!/bin/sh

if true; then echo “True Command”; else echo “Not A True Command”; fi

Output:

Example:

To execute the else part in the above script we can use Not (!) operator just before the true command.

#!/bin/sh

# Now it evaluate false 

if ! true; then echo “True Command”; else echo “Not A True Command”; fi

Output:

While statement:

We can use the true command in a while loop also. It is used to create infinite loops not only in shell scripting but also in other programming languages. 

Example:

#!/bin/sh

# while true: Print "Infinite loop"

while true; do 
  echo "Infinite loop"; 
done

Output:


Shell Scripting – True Command

A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix commands, we use a shell. The main job of a shell is to take commands from the user and convert them into the kernel’s understandable form. To summarize this, we can see it as a medium between a user and the kernel system of an OS. The kernel is a computer program that is considered the main part of a computer’s operating system.

This article focuses upon the shell scripting- True command.

Similar Reads

True command:

...

Verify exit status:

We can verify the exit statement of the true command but this command has to be used along with another command. For this purpose, a special shell variable (?) is used to store the status of the true command. This mechanism is illustrated in the below script....

If statement:

We can use the true command even in the if statement. This mechanism is illustrated in the below script....