Shell Scripting - True Command
Last Updated :
27 Jan, 2022
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.
True command:
This command is referred to as "Do nothing, successfully". This is because, on a UNIX-based operating system, the main purpose of this command is to return the successful exit status, which also means that it outputs nothing. It should be used if a part of a script always returns true. Its basic syntax is given below:
Syntax:
true [ arguments (optional) ]
It doesn't matter whether you provide arguments, the true command always returns successfully.
Exit status:
zero (0)
It signifies success
Example :
In this script, we have used true command without any argument.
#!/bin/sh
# true command without any argument
true
Output:
Example:
In this script, we are using the true command with an argument.
#!/bin/sh
# true command using an argument
true GeekforGeeks.txt
Output:

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.
Example:
#!/bin/sh
# Evaluates true then print the statement
true; echo "Status of the previous command is $?."
Output:

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:
Similar Reads
Shell Scripting - Shopt Command
Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Shell Scripting - Bash Trap Command
The trap command in Bash executes specific code in response to the signal given to the program. It traps the signal received by the program from the OS or the user and then executes the specific code provided by the programmer. It acts like a trigger in response to a signal. Just like try-catch stat
5 min read
Shell Scripting - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Scripting - Command Substitution
A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use the terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions
4 min read
Shell Scripting - Readonly Command
In this tutorial, we will learn about the âreadonlyâ command. Using the readonly command, you can assign a particular value to a variable that can neither be changed nor modified by subsequent assignments nor be unset. Syntax:readonly name[=value] readonly [-aAf] [name[=value] Most used options: -p
3 min read
Shell Scripting - Decision Making
A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can
7 min read
Shell Scripting - Subshell
Shell scripting is a powerful tool for automating tasks and simplifying the management of systems and applications. One important concept in shell scripting is the use of subshells, which allow you to execute commands within a separate shell environment. A subshell is a child shell that is spawned b
4 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying
5 min read
Shell Scripting - /dev/null
Anyone who starts with bash scripting will definitely come across /dev/null. It is a special type of file that every Linux distro has. It is a blackhole of the operating system where anything put into it is discarded. In this article, we will be going through the /dev/null Shell Scripting command wi
3 min read
Shell Scripting - Define #!/bin/bash
A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
3 min read