Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

true and false (commands)

From Wikipedia, the free encyclopedia
Standard Unix utility
true
Initial releaseJanuary 1979; 46 years ago (1979-01)
Operating systemUnix andUnix-like
PlatformCross-platform
TypeCommand

InUnix-likeoperating systems,true andfalse are commands whose only function is to always return with a predeterminedexit status. Programmers and scripts often use the exit status of a command to assess success (exit status zero) or failure (non-zero) of the command. Thetrue andfalse commands represent thelogical values of command success, becausetrue returns 0, andfalse returns 1.[Note 1]

Usage

[edit]

The commands are usually employed inconditional statements andloops ofshell scripts. For example, the following shell script repeats theecho hello loop until interrupted:

whiletruedoechohellodone

The commands can be used to ignore the success or failure of a sequence of other commands, as in the example:

make&&false

Setting a user'slogin shell tofalse, in/etc/passwd, effectively denies them access to an interactive shell, but their account may still be valid for other services, such asFTP. (Although/sbin/nologin, if available, may be more fitting for this purpose, as it prints a notification before terminating the session.)

The programs take no "actual" parameters; in the GNU version, the standard parameter--help displays a usage summary and--version displays the program version.

Null command

[edit]

Thetrue command is sometimes substituted with the very similar null command,[1] written as a single colon (:). The null command isbuilt into the shell, and may therefore be more efficient iftrue is an external program (true is usually a shell built in function). We can rewrite the upper example using: instead oftrue:

while:doechohellodone

The null command may take parameters, which are ignored. It is also used as ano-op dummy command for side-effects such as assigning default values toshell variables through the${parameter:=word} parameter expansion form.[2] For example, frombashbug, the bug-reporting script forBash:

:${TMPDIR:=/tmp}:${EDITOR=$DEFEDITOR}:${USER=${LOGNAME-`whoami`}}

See also

[edit]

Notes

[edit]
  1. ^These are distinct from thetruth values ofclassical logic and most general purpose programming languages:true (1 or T) andfalse (0 or ⊥).

References

[edit]
  1. ^"Colon",The Open group base specifications, issue 7, IEEE std 1003.1-2008
  2. ^Cooper, Mendel (April 2011),"Null command",Advanced Bash-scripting guide, 6.3, The Linux documentation project, retrieved2011-08-04

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands

Manual pages

[edit]
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=True_and_false_(commands)&oldid=1197776418"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp