Previous | Table of Contents | Next |
DESCRIPTION
The internal true command always returns an exit status of zero. It is used to guarantee a zero exit status (return code) for a conditional type statement. The shell considers a zero return code to be true and a nonzero return code to be false.
COMMAND FORMAT
Following are the general formats of the true command.
true :
|
|
---|---|
C Shell | |
[/bin/]true : (1) |
|
|
The colon command may also be used to return only a true exit status. In fact the Korn shell aliases the true command to : as shown in the following code:
alias true=':'
FURTHER DISCUSSION
The true command lends itself well to shell programming. It is a sure-bet command that always returns a status of zero. Thus you can program infinite loops and break out based on various conditions.
The most common code using the true command is probably the following:
while true do sleep 60 echo "Just another endless day(loop)!" done &
This code will run forever or until someone kills the process or shuts down the system. The following lines of shell code are equivalent:
while : do sleep 60 echo "Just another endless day(loop)!" done &
You could also code this with the until loop using the false command.
NOTE:
The true command returns a 0. The shell uses 0 as a true condition. Most programming languages, such as C, use 1 or nonzero numbers as true conditions. The reason for this convention is that return codes from programs are 0 if the program completed successfully and nonzero if a problem occurred.
RELATED COMMANDS
Refer to the false command described in Module 49.
RETURN CODES
The true command always returns a zero return code.
APPLICATIONS
The most common use of the true command is in looping conventions. You can use a true command on a while loop statement to create an infinite (endless) loop. Some other condition such as a case or if command must execute a break or exit to leave the loop. The true command may also be useful in testing shell scripts. You can place the true command on if and while loops to control the shell script and test how it performs in these conditions.
TYPICAL OPERATION
In this activity you use the true command to loop indefinitely while printing the time to your screen. Begin at the shell prompt.
CLR=`tput clear` while true do echo "{CLR}\n\n\n\n\n" banner " `date +%H:%M`" sleep 59 done
BSD (Berkeley) version |
---|
CLR=clear while (1) do echo " " echo " "`date` sleep 59 done
Previous | Table of Contents | Next |