3 @setfilename sh-utils.info
4 @settitle GNU shell utilities
9 @c Define new indices for file names and options.
13 @c Put everything in one index (arbitrarily chosen to be the concept index).
21 @set Francois Fran@,{c}ois
26 * Shell utilities: (sh-utils). GNU shell utilities.
27 * basename: (sh-utils)basename invocation. Strip directory and suffix.
28 * chroot: (sh-utils)chroot invocation. Specify the root directory.
29 * date: (sh-utils)date invocation. Print/set system date and time.
30 * dirname: (sh-utils)dirname invocation. Strip non-directory suffix.
31 * echo: (sh-utils)echo invocation. Print a line of text.
32 * env: (sh-utils)env invocation. Modify the environment.
33 * expr: (sh-utils)expr invocation. Evaluate expressions.
34 * factor: (sh-utils)factor invocation. Print prime factors
35 * false: (sh-utils)false invocation. Do nothing, unsuccessfully.
36 * groups: (sh-utils)groups invocation. Print group names a user is in.
37 * hostid: (sh-utils)hostid invocation. Print numeric host identifier.
38 * hostname: (sh-utils)hostname invocation. Print or set system name.
39 * id: (sh-utils)id invocation. Print real/effective uid/gid.
40 * logname: (sh-utils)logname invocation. Print current login name.
41 * nice: (sh-utils)nice invocation. Modify scheduling priority.
42 * nohup: (sh-utils)nohup invocation. Immunize to hangups.
43 * pathchk: (sh-utils)pathchk invocation. Check file name portability.
44 * printenv: (sh-utils)printenv invocation. Print environment variables.
45 * printf: (sh-utils)printf invocation. Format and print data.
46 * pwd: (sh-utils)pwd invocation. Print working directory.
47 * seq: (sh-utils)seq invocation. Print numeric sequences
48 * sleep: (sh-utils)sleep invocation. Delay for a specified time.
49 * stty: (sh-utils)stty invocation. Print/change terminal settings.
50 * su: (sh-utils)su invocation. Modify user and group id.
51 * tee: (sh-utils)tee invocation. Redirect to multiple files.
52 * test: (sh-utils)test invocation. File/string tests.
53 * true: (sh-utils)true invocation. Do nothing, successfully.
54 * tty: (sh-utils)tty invocation. Print terminal name.
55 * uname: (sh-utils)uname invocation. Print system information.
56 * users: (sh-utils)users invocation. Print current user names.
57 * who: (sh-utils)who invocation. Print who is logged in.
58 * whoami: (sh-utils)whoami invocation. Print effective user id.
59 * yes: (sh-utils)yes invocation. Print a string indefinitely.
65 This file documents the GNU shell utilities.
67 Copyright (C) 1994, 1995, 1996, 2000 Free Software Foundation, Inc.
69 Permission is granted to make and distribute verbatim copies of
70 this manual provided the copyright notice and this permission notice
71 are preserved on all copies.
74 Permission is granted to process this file through TeX and print the
75 results, provided the printed document carries copying permission
76 notice identical to this one except for the removal of this paragraph
77 (this paragraph not being relevant to the printed manual).
80 Permission is granted to copy and distribute modified versions of this
81 manual under the conditions for verbatim copying, provided that the entire
82 resulting derived work is distributed under the terms of a permission
83 notice identical to this one.
85 Permission is granted to copy and distribute translations of this manual
86 into another language, under the above conditions for modified versions,
87 except that this permission notice may be stated in a translation approved
92 @title GNU @code{sh-utils}
93 @subtitle A set of shell utilities
94 @subtitle for version @value{VERSION}, @value{UPDATED}
95 @author David MacKenzie et al.
98 @vskip 0pt plus 1filll
99 Copyright @copyright{} 1994, 1995, 1996, 2000 Free Software Foundation, Inc.
101 Permission is granted to make and distribute verbatim copies of
102 this manual provided the copyright notice and this permission notice
103 are preserved on all copies.
105 Permission is granted to copy and distribute modified versions of this
106 manual under the conditions for verbatim copying, provided that the entire
107 resulting derived work is distributed under the terms of a permission
108 notice identical to this one.
110 Permission is granted to copy and distribute translations of this manual
111 into another language, under the above conditions for modified versions,
112 except that this permission notice may be stated in a translation approved
119 @top GNU shell utilities
121 @cindex shell utilities
122 @cindex utilities for shell programming
124 This manual documents version @value{VERSION} of the GNU shell utilities.
127 * Introduction:: Caveats, overview, and authors.
128 * Common options:: Common options.
130 * Date input formats:: Specifying date strings.
131 * Printing text:: echo printf yes
132 * Conditions:: false true test expr
134 * File name manipulation:: dirname basename pathchk
135 * Working context:: pwd stty printenv tty
136 * User information:: id logname whoami groups users who
137 * System context:: date uname hostname
138 * Modified command invocation:: chroot env nice nohup su
140 * Numeric operations:: factor seq
142 * Index:: General index.
148 @chapter Introduction
152 First of all, this manual is incomplete. The @code{stty} section, in
153 particular, needs substantial reorganization and additional explanatory
154 text before it will be up to the standard of other GNU manuals.
155 Explanatory text in general is lacking; the manual presently assumes you
156 pretty much know what to do, and just need to be reminded of how. Thus,
157 if you are interested, please get involved in improving this manual.
158 The entire GNU community will benefit.
160 Some of these programs are useful only when writing shell scripts;
161 utilities like these are, in fact, the ``language'' of shell scripts (to
162 a great extent). Others are occasionally useful interactively.
165 The GNU shell utilities are mostly compatible with the POSIX.2 standard.
167 @c This paragraph appears in all of fileutils.texi, textutils.texi, and
168 @c sh-utils.texi too -- so be sure to keep them consistent.
169 @cindex bugs, reporting
170 Please report bugs to @samp{bug-sh-utils@@gnu.org}. Remember
171 to include the version number, machine architecture, input files, and
172 any other information needed to reproduce the bug: your input, what you
173 expected, what you got, and why it is wrong. Diffs are welcome, but
174 please include a description of the problem as well, since this is
175 sometimes difficult to infer. @xref{Bugs, , , gcc, GNU CC}.
178 @cindex MacKenzie, David
179 @cindex Meyering, Jim
180 @c Sorry, but the @value trick doesn't work with TeX in indexing
181 @c commands, and I don't want to fix it right now. --karl.
182 @cindex Pinard, @value{Francois}
184 @cindex Stallman, Richard
185 This manual was originally derived from the Unix man pages in the
186 distribution, which were written by David MacKenzie and updated by Jim
187 Meyering. What you are reading now is the authoritative documentation
188 for these utilities; the man pages are no longer being maintained.
189 @value{Francois} Pinard did the initial conversion to Texinfo format.
190 Karl Berry did the indexing, some reorganization, and editing of the results.
191 Richard Stallman contributed his usual invaluable insights to the
196 @chapter Common options
198 @cindex common options
200 Certain options are available in all these programs. Rather than
201 writing identical descriptions for each of the programs, they are
202 described here. (In fact, every GNU program accepts (or should accept)
205 Many of these programs take arbitrary strings as arguments. In those
206 cases, @samp{--help} and @samp{--version} are taken as these options
207 only if there is one and exactly one command line argument.
214 Print a usage message listing all available options, then exit successfully.
218 @cindex version number, finding
219 Print the version number, then exit successfully.
224 @include getdate.texi
228 @chapter Printing text
230 @cindex printing text, commands for
231 @cindex commands for printing text
233 This section describes commands that display text strings.
236 * echo invocation:: Print a line of text.
237 * printf invocation:: Format and print data.
238 * yes invocation:: Print a string until interrupted.
242 @node echo invocation
243 @section @code{echo}: Print a line of text
246 @cindex displaying text
247 @cindex printing text
248 @cindex text, displaying
249 @cindex arbitrary text, displaying
251 @code{echo} writes each given @var{string} to standard output, with a
252 space between each and a newline after the last one. Synopsis:
255 echo [@var{option}]@dots{} [@var{string}]@dots{}
258 The program accepts the following options. Also see @ref{Common options}.
263 Do not output the trailing newline.
267 @cindex backslash escapes
268 Enable interpretation of the following backslash-escaped characters in
277 suppress trailing newline
291 the character whose ASCII code is @var{nnn} (octal); if @var{nnn} is not
292 a valid octal number, it is printed literally.
298 @node printf invocation
299 @section @code{printf}: Format and print data
302 @code{printf} does formatted printing of text. Synopsis:
305 printf @var{format} [@var{argument}]@dots{}
308 @code{printf} prints the @var{format} string, interpreting @samp{%}
309 directives and @samp{\} escapes in the same way as the C @code{printf}
310 function. The @var{format} argument is re-used as necessary to convert
311 all of the given @var{argument}s.
313 @code{printf} has one additional directive, @samp{%b}, which prints its
314 argument string with @samp{\} escapes interpreted in the same way as in
315 the @var{format} string.
319 @code{printf} interprets @samp{\0ooo} in @var{format} as an octal number
320 (if @var{ooo} is 0 to 3 octal digits) specifying a character to print,
321 and @samp{\xhhh} as a hexadecimal number (if @var{hhh} is 1 to 3 hex
322 digits) specifying a character to print.
326 @code{printf} interprets two character syntaxes introduced in ISO C 99:
327 @samp{\u} for 16-bit Unicode characters, specified as 4 hex digits
328 @var{hhhh}, and @samp{\U} for 32-bit Unicode characters, specified as 8 hex
329 digits @var{hhhhhhhh}. @code{printf} outputs the Unicode characters
330 according to the LC_CTYPE part of the current locale, i.e. depending
331 on the values of the environment variables @code{LC_ALL}, @code{LC_CTYPE},
334 The processing of @samp{\u} and @samp{\U} requires a full-featured
335 @code{iconv} facility. It is activated on systems with glibc 2.2 (or newer),
336 or when @code{libiconv} is installed prior to the sh-utils. Otherwise the
337 use of @samp{\u} and @samp{\U} will give an error message.
340 An additional escape, @samp{\c}, causes @code{printf} to produce no
343 The only options are a lone @samp{--help} or
344 @samp{--version}. @xref{Common options}.
346 The Unicode character syntaxes are useful for writing strings in a locale
347 independent way. For example, a string containing the Euro currency symbol
350 $ /usr/local/bin/printf '\u20AC 14.95'
354 will be output correctly in all locales supporting the Euro symbol
355 (ISO-8859-15, UTF-8, and others). Similarly, a Chinese string
358 $ /usr/local/bin/printf '\u4e2d\u6587'
362 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
364 Note that in these examples, the full pathname of @code{printf} has been
365 given, to distinguish it from the GNU @code{bash} builtin function
368 For larger strings, you don't need to look up the hexadecimal code
369 values of each character one by one. ASCII characters mixed with \u
370 escape sequences is also known as the JAVA source file encoding. You can
371 use GNU recode 3.5c (or newer) to convert strings to this encoding. Here
372 is how to convert a piece of text into a shell script which will output
373 this text in a locale-independent way:
376 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
377 '\u4e2d\u6587\n' > sample.txt
378 $ recode BIG5..JAVA < sample.txt \
379 | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
385 @section @code{yes}: Print a string until interrupted
388 @cindex repeated output of a string
390 @code{yes} prints the command line arguments, separated by spaces and
391 followed by a newline, forever until it is killed. If no arguments are
392 given, it prints @samp{y} followed by a newline forever until killed.
394 The only options are a lone @samp{--help} or @samp{--version}.
395 @xref{Common options}.
402 @cindex commands for exit status
403 @cindex exit status commands
405 This section describes commands that are primarily useful for their exit
406 status, rather than their output. Thus, they are often used as the
407 condition of shell @code{if} statements, or as the last command in a
411 * false invocation:: Do nothing, unsuccessfully.
412 * true invocation:: Do nothing, successfully.
413 * test invocation:: Check file types and compare values.
414 * expr invocation:: Evaluate expressions.
418 @node false invocation
419 @section @code{false}: Do nothing, unsuccessfully
422 @cindex do nothing, unsuccessfully
423 @cindex failure exit status
424 @cindex exit status of @code{false}
426 @code{false} does nothing except return an exit status of 1, meaning
427 @dfn{failure}. It can be used as a place holder in shell scripts
428 where an unsuccessful command is needed.
430 By default, @code{false} honors the @samp{--help} and @samp{--version}
431 options. However, that is contrary to @sc{POSIX}, so when the environment
432 variable @env{POSIXLY_CORRECT} is set, @code{false} ignores @emph{all}
433 command line arguments, including @samp{--help} and @samp{--version}.
435 This version of @code{false} is implemented as a C program, and is thus
436 more secure and faster than a shell script implementation, and may safely
437 be used as a dummy shell for the purpose of disabling accounts.
440 @node true invocation
441 @section @code{true}: Do nothing, successfully
444 @cindex do nothing, successfully
446 @cindex successful exit
447 @cindex exit status of @code{true}
449 @code{true} does nothing except return an exit status of 0, meaning
450 @dfn{success}. It can be used as a place holder in shell scripts
451 where a successful command is needed, although the shell built-in
452 command @code{:} (colon) may do the same thing faster.
453 In most modern shells, @code{true} is built-in command, so when
454 you use @samp{true} in a script, you're probably using the built-in
455 command, not the one documented here.
457 By default, @code{true} honors the @samp{--help} and @samp{--version}
458 options. However, that is contrary to @sc{POSIX}, so when the environment
459 variable @env{POSIXLY_CORRECT} is set, @code{true} ignores @emph{all}
460 command line arguments, including @samp{--help} and @samp{--version}.
462 This version of @code{true} is implemented as a C program, and is thus
463 more secure and faster than a shell script implementation, and may safely
464 be used as a dummy shell for the purpose of disabling accounts.
467 @node test invocation
468 @section @code{test}: Check file types and compare values
471 @cindex check file types
472 @cindex compare values
473 @cindex expression evaluation
475 @code{test} returns a status of 0 (true) or 1 (false) depending on the
476 evaluation of the conditional expression @var{expr}. Each part of the
477 expression must be a separate argument.
479 @code{test} has file status checks, string operators, and numeric
480 comparison operators.
482 @cindex conflicts with shell built-ins
483 @cindex built-in shell commands, conflicts with
484 Because most shells have a built-in command by the same name, using the
485 unadorned command name in a script or interactively may get you
486 different functionality than that described here.
488 Besides the options below, @code{test} accepts a lone @samp{--help} or
489 @samp{--version}. @xref{Common options}. A single non-option argument
490 is also allowed: @code{test} returns true if the argument is not null.
493 * File type tests:: -[bcdfhLpSt]
494 * Access permission tests:: -[gkruwxOG]
495 * File characteristics tests:: -e -s -nt -ot -ef
496 * String tests:: -z -n = !=
497 * Numeric tests:: -eq -ne -lt -le -gt -ge
498 * Connectives for test:: ! -a -o
502 @node File type tests
503 @subsection File type tests
505 @cindex file type tests
507 These options test for particular types of files. (Everything's a file,
508 but not all files are the same!)
514 @cindex block special check
515 True if @var{file} exists and is a block special device.
519 @cindex character special check
520 True if @var{file} exists and is a character special device.
524 @cindex directory check
525 True if @var{file} exists and is a directory.
529 @cindex regular file check
530 True if @var{file} exists and is a regular file.
536 @cindex symbolic link check
537 True if @var{file} exists and is a symbolic link.
541 @cindex named pipe check
542 True if @var{file} exists and is a named pipe.
547 True if @var{file} exists and is a socket.
551 @cindex terminal check
552 True if @var{fd} is opened on a terminal. If @var{fd} is omitted, it
553 defaults to 1 (standard output).
558 @node Access permission tests
559 @subsection Access permission tests
561 @cindex access permission tests
562 @cindex permission tests
564 These options test for particular access permissions.
570 @cindex set-group-id check
571 True if @var{file} exists and has its set-group-id bit set.
575 @cindex sticky bit check
576 True if @var{file} has its @dfn{sticky} bit set.
580 @cindex readable file check
581 True if @var{file} exists and is readable.
585 @cindex set-user-id check
586 True if @var{file} exists and has its set-user-id bit set.
590 @cindex writable file check
591 True if @var{file} exists and is writable.
595 @cindex executable file check
596 True if @var{file} exists and is executable.
600 @cindex owned by effective uid check
601 True if @var{file} exists and is owned by the current effective user id.
605 @cindex owned by effective gid check
606 True if @var{file} exists and is owned by the current effective group id.
610 @node File characteristics tests
611 @subsection File characteristics tests
613 @cindex file characteristics tests
615 These options test other file characteristics.
621 @cindex existence-of-file check
622 True if @var{file} exists.
626 @cindex nonempty file check
627 True if @var{file} exists and has a size greater than zero.
629 @item @var{file1} -nt @var{file2}
631 @cindex newer-than file check
632 True if @var{file1} is newer (according to modification date) than
635 @item @var{file1} -ot @var{file2}
637 @cindex older-than file check
638 True if @var{file1} is older (according to modification date) than
641 @item @var{file1} -ef @var{file2}
643 @cindex same file check
644 @cindex hard link check
645 True if @var{file1} and @var{file2} have the same device and inode
646 numbers, i.e., if they are hard links to each other.
652 @subsection String tests
656 These options test string characteristics. Strings are not quoted for
657 @code{test}, though you may need to quote them to protect characters
658 with special meaning to the shell, e.g., spaces.
662 @item -z @var{string}
664 @cindex zero-length string check
665 True if the length of @var{string} is zero.
667 @item -n @var{string}
670 @cindex nonzero-length string check
671 True if the length of @var{string} is nonzero.
673 @item @var{string1} = @var{string2}
675 @cindex equal string check
676 True if the strings are equal.
678 @item @var{string1} != @var{string2}
680 @cindex not-equal string check
681 True if the strings are not equal.
687 @subsection Numeric tests
689 @cindex numeric tests
690 @cindex arithmetic tests
692 Numeric relationals. The arguments must be entirely numeric (possibly
693 negative), or the special expression @w{@code{-l @var{string}}}, which
694 evaluates to the length of @var{string}.
698 @item @var{arg1} -eq @var{arg2}
699 @itemx @var{arg1} -ne @var{arg2}
700 @itemx @var{arg1} -lt @var{arg2}
701 @itemx @var{arg1} -le @var{arg2}
702 @itemx @var{arg1} -gt @var{arg2}
703 @itemx @var{arg1} -ge @var{arg2}
710 These arithmetic binary operators return true if @var{arg1} is equal,
711 not-equal, less-than, less-than-or-equal, greater-than, or
712 greater-than-or-equal than @var{arg2}, respectively.
719 test -1 -gt -2 && echo yes
721 test -l abc -gt 1 && echo yes
724 @error{} test: integer expression expected before -eq
728 @node Connectives for test
729 @subsection Connectives for @code{test}
731 @cindex logical connectives
732 @cindex connectives, logical
734 The usual logical connectives.
740 True if @var{expr} is false.
742 @item @var{expr1} -a @var{expr2}
744 @cindex logical and operator
746 True if both @var{expr1} and @var{expr2} are true.
748 @item @var{expr1} -o @var{expr2}
750 @cindex logical or operator
752 True if either @var{expr1} or @var{expr2} is true.
757 @node expr invocation
758 @section @code{expr}: Evaluate expressions
761 @cindex expression evaluation
762 @cindex evaluation of expressions
764 @code{expr} evaluates an expression and writes the result on standard
765 output. Each token of the expression must be a separate argument.
767 Operands are either numbers or strings. @code{expr} converts
768 anything appearing in an operand position to an integer or a string
769 depending on the operation being applied to it.
771 Strings are not quoted for @code{expr} itself, though you may need to
772 quote them to protect characters with special meaning to the shell,
775 @cindex parentheses for grouping
776 Operators may given as infix symbols or prefix keywords. Parentheses
777 may be used for grouping in the usual manner (you must quote parentheses
778 to avoid the shell evaluating them, however).
780 @cindex exit status of @code{expr}
784 0 if the expression is neither null nor 0,
785 1 if the expression is null or 0,
786 2 for invalid expressions.
790 * String expressions:: <colon> match substr index length quote
791 * Numeric expressions:: + - * / %
792 * Relations for expr:: | & < <= = == != >= >
793 * Examples of expr:: Examples.
797 @node String expressions
798 @subsection String expressions
800 @cindex string expressions
801 @cindex expressions, string
803 @code{expr} supports pattern matching and other string operators. These
804 have lower precedence than both the numeric and relational operators (in
809 @item @var{string} : @var{regex}
810 @cindex pattern matching
811 @cindex regular expression matching
812 @cindex matching patterns
813 Perform pattern matching. The arguments are converted to strings and the
814 second is considered to be a (basic, a la GNU @code{grep}) regular
815 expression, with a @code{^} implicitly prepended. The first argument is
816 then matched against this regular expression.
818 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
819 @code{:} expression returns the part of @var{string} that matched the
820 subexpression; otherwise, it returns the number of characters matched.
822 If the match fails, the @code{:} operator returns the null string if
823 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
825 @kindex \( @r{regexp operator}
826 Only the first @samp{\( @dots{} \)} pair is relevant to the return
827 value; additional pairs are meaningful only for grouping the regular
828 expression operators.
830 @kindex \+ @r{regexp operator}
831 @kindex \? @r{regexp operator}
832 @kindex \| @r{regexp operator}
833 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
834 operators which respectively match one or more, zero or one, or separate
835 alternatives. SunOS and other @code{expr}'s treat these as regular
836 characters. (POSIX allows either behavior.)
837 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
838 regular expression syntax. Some examples are in @ref{Examples of expr}.
840 @item match @var{string} @var{regex}
842 An alternative way to do pattern matching. This is the same as
843 @w{@samp{@var{string} : @var{regex}}}.
845 @item substr @var{string} @var{position} @var{length}
847 Returns the substring of @var{string} beginning at @var{position}
848 with length at most @var{length}. If either @var{position} or
849 @var{length} is negative, zero, or non-numeric, returns the null string.
851 @item index @var{string} @var{charset}
853 Returns the first position in @var{string} where the first character in
854 @var{charset} was found. If no character in @var{charset} is found in
855 @var{string}, return 0.
857 @item length @var{string}
859 Returns the length of @var{string}.
861 @item quote @var{token}
863 Interpret @var{token} as a string, even if it is a keyword like @var{match}
864 or an operator like @code{/}.
865 This makes it possible to test @code{expr length quote "$x"} or
866 @code{expr quote "$x" : '.*/\(.\)'} and have it do the right thing even if
867 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
868 This operator is a GNU extension. It is disabled when
869 the environment variable @env{POSIXLY_CORRECT} is set.
873 To make @code{expr} interpret keywords as strings, you must use the
874 @code{quote} operator.
877 @node Numeric expressions
878 @subsection Numeric expressions
880 @cindex numeric expressions
881 @cindex expressions, numeric
883 @code{expr} supports the usual numeric operators, in order of increasing
884 precedence. The string operators (previous section) have lower precedence,
885 the connectives (next section) have higher.
894 Addition and subtraction. Both arguments are converted to numbers;
895 an error occurs if this cannot be done.
901 @cindex multiplication
904 Multiplication, division, remainder. Both arguments are converted to
905 numbers; an error occurs if this cannot be done.
910 @node Relations for expr
911 @subsection Relations for @code{expr}
913 @cindex connectives, logical
914 @cindex logical connectives
915 @cindex relations, numeric or string
917 @code{expr} supports the usual logical connectives and relations. These
918 are higher precedence than either the string or numeric operators
919 (previous sections). Here is the list, lowest-precedence operator first.
925 @cindex logical or operator
927 Returns its first argument if that is neither null nor 0, otherwise its
932 @cindex logical and operator
934 Return its first argument if neither argument is null or 0, otherwise
937 @item < <= = == != >= >
944 @cindex comparison operators
945 Compare the arguments and return 1 if the relation is true, 0 otherwise.
946 @code{==} is a synonym for @code{=}. @code{expr} first tries to convert
947 both arguments to numbers and do a numeric comparison; if either
948 conversion fails, it does a lexicographic comparison.
953 @node Examples of expr
954 @subsection Examples of using @code{expr}
956 @cindex examples of @code{expr}
957 Here are a few examples, including quoting for shell metacharacters.
959 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
964 To print the non-directory part of the file name stored in
965 @code{$fname}, which need not contain a @code{/}.
967 expr $fname : '.*/\(^.*\)' '^|' $fname
970 An example showing that @code{\+} is an operator:
982 @error{} expr: syntax error
983 expr index quote index a
992 @cindex commands for redirection
994 Unix shells commonly provide several forms of @dfn{redirection}---ways
995 to change the input source or output destination of a command. But one
996 useful redirection is performed by a separate command, not by the shell;
1000 * tee invocation:: Redirect output to multiple files.
1004 @node tee invocation
1005 @section @code{tee}: Redirect output to multiple files
1008 @cindex pipe fitting
1009 @cindex destinations, multiple output
1010 @cindex read from stdin and write to stdout and files
1012 The @code{tee} command copies standard input to standard output and also
1013 to any files given as arguments. This is useful when you want not only
1014 to send some data down a pipe, but also to save a copy. Synopsis:
1017 tee [@var{option}]@dots{} [@var{file}]@dots{}
1020 If a file being written to does not already exist, it is created. If a
1021 file being written to already exists, the data it previously contained
1022 is overwritten unless the @code{-a} option is used.
1024 The program accepts the following options. Also see @ref{Common options}.
1031 Append standard input to the given files rather than overwriting
1035 @itemx --ignore-interrupts
1037 @opindex --ignore-interrupts
1038 Ignore interrupt signals.
1043 @node File name manipulation
1044 @chapter File name manipulation
1046 @cindex file name manipulation
1047 @cindex manipulation of file names
1048 @cindex commands for file name manipulation
1050 This section describes commands that manipulate file names.
1053 * basename invocation:: Strip directory and suffix from a file name.
1054 * dirname invocation:: Strip non-directory suffix from a file name.
1055 * pathchk invocation:: Check file name portability.
1059 @node basename invocation
1060 @section @code{basename}: Strip directory and suffix from a file name
1063 @cindex strip directory and suffix from file names
1064 @cindex directory, stripping from file names
1065 @cindex suffix, stripping from file names
1066 @cindex file names, stripping directory and suffix
1067 @cindex leading directory components, stripping
1069 @code{basename} removes any leading directory components from
1070 @var{name}. Synopsis:
1073 basename @var{name} [@var{suffix}]
1076 If @var{suffix} is specified and is identical to the end of @var{name},
1077 it is removed from @var{name} as well. @code{basename} prints the
1078 result on standard output.
1080 The only options are @samp{--help} and @samp{--version}. @xref{Common
1084 @node dirname invocation
1085 @section @code{dirname}: Strip non-directory suffix from a file name
1088 @cindex directory components, printing
1089 @cindex stripping non-directory suffix
1090 @cindex non-directory suffix, stripping
1092 @code{dirname} prints all but the final slash-delimited component of
1093 a string (presumably a filename). Synopsis:
1099 If @var{name} is a single component, @code{dirname} prints @samp{.}
1100 (meaning the current directory).
1102 The only options are @samp{--help} and @samp{--version}. @xref{Common
1106 @node pathchk invocation
1107 @section @code{pathchk}: Check file name portability
1110 @cindex file names, checking validity and portability
1111 @cindex valid file names, checking for
1112 @cindex portable file names, checking for
1114 @code{pathchk} checks portability of filenames. Synopsis:
1117 pathchk [@var{option}]@dots{} @var{name}@dots{}
1120 For each @var{name}, @code{pathchk} prints a message if any of
1121 these conditions is true:
1124 one of the existing directories in @var{name} does not have search
1125 (execute) permission,
1127 the length of @var{name} is larger than its filesystem's maximum
1130 the length of one component of @var{name}, corresponding to an
1131 existing directory name, is larger than its filesystem's maximum
1132 length for a file name component.
1135 The program accepts the following option. Also see @ref{Common options}.
1140 @itemx --portability
1142 @opindex --portability
1143 Instead of performing length checks on the underlying filesystem,
1144 test the length of each file name and its components against the
1145 POSIX.1 minimum limits for portability. Also check that the file
1146 name contains no characters not in the portable file name character set.
1150 @cindex exit status of @code{pathchk}
1154 0 if all specified file names passed all of the tests,
1159 @node Working context
1160 @chapter Working context
1162 @cindex working context
1163 @cindex commands for printing the working context
1165 This section describes commands that display or alter the context in
1166 which you are working: the current directory, the terminal settings, and
1167 so forth. See also the user-related commands in the next section.
1170 * pwd invocation:: Print working directory.
1171 * stty invocation:: Print or change terminal characteristics.
1172 * printenv invocation:: Print environment variables.
1173 * tty invocation:: Print file name of terminal on standard input.
1177 @node pwd invocation
1178 @section @code{pwd}: Print working directory
1181 @cindex print name of current directory
1182 @cindex current working directory, printing
1183 @cindex working directory, printing
1185 @cindex symbolic links and @code{pwd}
1186 @code{pwd} prints the fully resolved name of the current directory.
1187 That is, all components of the printed name will be actual directory
1188 names---none will be symbolic links.
1190 @cindex conflicts with shell built-ins
1191 @cindex built-in shell commands, conflicts with
1192 Because most shells have a built-in command by the same name, using the
1193 unadorned command name in a script or interactively may get you
1194 different functionality than that described here.
1196 The only options are a lone @samp{--help} or
1197 @samp{--version}. @xref{Common options}.
1200 @node stty invocation
1201 @section @code{stty}: Print or change terminal characteristics
1204 @cindex change or print terminal settings
1205 @cindex terminal settings
1206 @cindex line settings of terminal
1208 @code{stty} prints or changes terminal characteristics, such as baud rate.
1212 stty [@var{option}] [@var{setting}]@dots{}
1216 If given no line settings, @code{stty} prints the baud rate, line
1217 discipline number (on systems that support it), and line settings
1218 that have been changed from the values set by @samp{stty sane}.
1219 By default, mode reading and setting are performed on the tty line
1220 connected to standard input, although this can be modified by the
1221 @samp{--file} option.
1223 @code{stty} accepts many non-option arguments that change aspects of
1224 the terminal line operation, as described below.
1226 The program accepts the following options. Also see @ref{Common options}.
1233 Print all current settings in human-readable form. This option may not
1234 be used in combination with any line settings.
1236 @item -F @var{device}
1237 @itemx --file=@var{device}
1240 Set the line opened by the filename specified in @var{device} instead of
1241 the tty line connected to standard input. This option is necessary
1242 because opening a POSIX tty requires use of the @code{O_NONDELAY} flag to
1243 prevent a POSIX tty from blocking until the carrier detect line is high if
1244 the @code{clocal} flag is not set. Hence, it is not always possible
1245 to allow the shell to open the device in the traditional manner.
1251 @cindex machine-readable @code{stty} output
1252 Print all current settings in a form that can be used as an argument to
1253 another @code{stty} command to restore the current settings. This option
1254 may not be used in combination with any line settings.
1258 Many settings can be turned off by preceding them with a @samp{-}.
1259 Such arguments are marked below with ``May be negated'' in their
1260 description. The descriptions themselves refer to the positive
1261 case, that is, when @emph{not} negated (unless stated otherwise,
1264 Some settings are not available on all POSIX systems, since they use
1265 extensions. Such arguments are marked below with ``Non-POSIX'' in their
1266 description. On non-POSIX systems, those or other settings also may not
1267 be available, but it's not feasible to document all the variations: just
1271 * Control:: Control settings
1272 * Input:: Input settings
1273 * Output:: Output settings
1274 * Local:: Local settings
1275 * Combination:: Combination settings
1276 * Characters:: Special characters
1277 * Special:: Special settings
1282 @subsection Control settings
1284 @cindex control settings
1290 @cindex two-way parity
1291 Generate parity bit in output and expect parity bit in input.
1298 Set odd parity (even if negated). May be negated.
1305 @cindex character size
1306 @cindex eight-bit characters
1307 Set character size to 5, 6, 7, or 8 bits.
1312 Send a hangup signal when the last process closes the tty. May be
1318 Use two stop bits per character (one if negated). May be negated.
1322 Allow input to be received. May be negated.
1326 @cindex modem control
1327 Disable modem control signals. May be negated.
1331 @cindex hardware flow control
1332 @cindex flow control, hardware
1333 @cindex RTS/CTS flow control
1334 Enable RTS/CTS flow control. Non-POSIX. May be negated.
1339 @subsection Input settings
1341 @cindex input settings
1346 @cindex breaks, ignoring
1347 Ignore break characters. May be negated.
1351 @cindex breaks, cause interrupts
1352 Make breaks cause an interrupt signal. May be negated.
1356 @cindex parity, ignoring
1357 Ignore characters with parity errors. May be negated.
1361 @cindex parity errors, marking
1362 Mark parity errors (with a 255-0-character sequence). May be negated.
1366 Enable input parity checking. May be negated.
1370 @cindex eight-bit input
1371 Clear high (8th) bit of input characters. May be negated.
1375 @cindex newline, translating to return
1376 Translate newline to carriage return. May be negated.
1380 @cindex return, ignoring
1381 Ignore carriage return. May be negated.
1385 @cindex return, translating to newline
1386 Translate carriage return to newline. May be negated.
1390 @kindex C-s/C-q flow control
1391 @cindex XON/XOFF flow control
1392 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}). May
1399 @cindex software flow control
1400 @cindex flow control, software
1401 Enable sending of @code{stop} character when the system input buffer
1402 is almost full, and @code{start} character when it becomes almost
1403 empty again. May be negated.
1407 @cindex uppercase, translating to lowercase
1408 Translate uppercase characters to lowercase. Non-POSIX. May be
1413 Allow any character to restart output (only the start character
1414 if negated). Non-POSIX. May be negated.
1418 @cindex beeping at input buffer full
1419 Enable beeping and not flushing input buffer if a character arrives
1420 when the input buffer is full. Non-POSIX. May be negated.
1425 @subsection Output settings
1427 @cindex output settings
1428 These arguments specify output-related operations.
1433 Postprocess output. May be negated.
1437 @cindex lowercase, translating to output
1438 Translate lowercase characters to uppercase. Non-POSIX. May be
1443 @cindex return, translating to newline
1444 Translate carriage return to newline. Non-POSIX. May be negated.
1448 @cindex newline, translating to crlf
1449 Translate newline to carriage return-newline. Non-POSIX. May be
1454 Do not print carriage returns in the first column. Non-POSIX.
1459 Newline performs a carriage return. Non-POSIX. May be negated.
1463 @cindex pad instead of timing for delaying
1464 Use fill (padding) characters instead of timing for delays. Non-POSIX.
1469 @cindex pad character
1470 Use delete characters for fill instead of null characters. Non-POSIX.
1476 Newline delay style. Non-POSIX.
1483 Carriage return delay style. Non-POSIX.
1490 Horizontal tab delay style. Non-POSIX.
1495 Backspace delay style. Non-POSIX.
1500 Vertical tab delay style. Non-POSIX.
1505 Form feed delay style. Non-POSIX.
1510 @subsection Local settings
1512 @cindex local settings
1517 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
1518 characters. May be negated.
1522 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
1523 special characters. May be negated.
1527 Enable non-POSIX special characters. May be negated.
1531 Echo input characters. May be negated.
1537 Echo @code{erase} characters as backspace-space-backspace. May be
1542 @cindex newline echoing after @code{kill}
1543 Echo a newline after a @code{kill} character. May be negated.
1547 @cindex newline, echoing
1548 Echo newline even if not echoing other characters. May be negated.
1552 @cindex flushing, disabling
1553 Disable flushing after @code{interrupt} and @code{quit} special
1554 characters. May be negated.
1558 @cindex case translation
1559 Enable input and output of uppercase characters by preceding their
1560 lowercase equivalents with @samp{\}, when @code{icanon} is set.
1561 Non-POSIX. May be negated.
1565 @cindex background jobs, stopping at terminal write
1566 Stop background jobs that try to write to the terminal. Non-POSIX.
1573 Echo erased characters backward, between @samp{\} and @samp{/}.
1574 Non-POSIX. May be negated.
1580 @cindex control characters, using @samp{^@var{c}}
1581 @cindex hat notation for control characters
1582 Echo control characters in hat notation (@samp{^@var{c}}) instead
1583 of literally. Non-POSIX. May be negated.
1589 Echo the @code{kill} special character by erasing each character on
1590 the line as indicated by the @code{echoprt} and @code{echoe} settings,
1591 instead of by the @code{echoctl} and @code{echok} settings. Non-POSIX.
1597 @subsection Combination settings
1599 @cindex combination settings
1600 Combination settings:
1607 Same as @code{parenb -parodd cs7}. May be negated. If negated, same
1608 as @code{-parenb cs8}.
1612 Same as @code{parenb parodd cs7}. May be negated. If negated, same
1613 as @code{-parenb cs8}.
1617 Same as @code{-icrnl -onlcr}. May be negated. If negated, same as
1618 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
1622 Reset the @code{erase} and @code{kill} special characters to their default
1628 @c This is too long to write inline.
1630 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
1631 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
1632 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
1633 ff0 isig icanon iexten echo echoe echok -echonl
1634 -noflsh -xcase -tostop -echoprt echoctl echoke
1636 @noindent and also sets all special characters to their default values.
1640 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
1641 sets the @code{eof} and @code{eol} characters to their default values
1642 if they are the same as the @code{min} and @code{time} characters.
1643 May be negated. If negated, same as @code{raw}.
1649 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
1650 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
1651 -imaxbel -opost -isig -icanon -xcase min 1 time 0
1653 @noindent May be negated. If negated, same as @code{cooked}.
1657 Same as @code{-icanon}. May be negated. If negated, same as
1662 @cindex eight-bit characters
1663 Same as @code{-parenb -istrip cs8}. May be negated. If negated,
1664 same as @code{parenb istrip cs7}.
1668 Same as @code{-parenb -istrip -opost cs8}. May be negated.
1669 If negated, same as @code{parenb istrip opost cs7}.
1673 Same as @code{-ixany}. Non-POSIX. May be negated.
1677 Same as @code{tab0}. Non-POSIX. May be negated. If negated, same
1684 Same as @code{xcase iuclc olcuc}. Non-POSIX. May be negated.
1688 Same as @code{echoe echoctl echoke}.
1692 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
1697 @subsection Special characters
1699 @cindex special characters
1700 @cindex characters, special
1702 The special characters' default values vary from system to system.
1703 They are set with the syntax @samp{name value}, where the names are
1704 listed below and the value can be given either literally, in hat
1705 notation (@samp{^@var{c}}), or as an integer which may start with
1706 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
1707 any other digit to indicate decimal.
1709 @cindex disabling special characters
1710 @kindex u@r{, and disabling special characters}
1711 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
1712 special character. (This is incompatible with Ultrix @code{stty},
1713 which uses a value of @samp{u} to disable a special character. GNU
1714 @code{stty} treats a value @samp{u} like any other, namely to set that
1715 special character to @key{U}.)
1721 Send an interrupt signal.
1729 Erase the last character typed.
1733 Erase the current line.
1737 Send an end of file (terminate the input).
1745 Alternate character to end the line. Non-POSIX.
1749 Switch to a different shell layer. Non-POSIX.
1753 Restart the output after stopping it.
1761 Send a terminal stop signal.
1765 Send a terminal stop signal after flushing the input. Non-POSIX.
1769 Redraw the current line. Non-POSIX.
1773 Erase the last word typed. Non-POSIX.
1777 Enter the next character typed literally, even if it is a special
1778 character. Non-POSIX.
1783 @subsection Special settings
1785 @cindex special settings
1790 Set the minimum number of characters that will satisfy a read until
1791 the time value has expired, when @code{-icanon} is set.
1795 Set the number of tenths of a second before reads time out if the minimum
1796 number of characters have not been read, when @code{-icanon} is set.
1798 @item ispeed @var{n}
1800 Set the input speed to @var{n}.
1802 @item ospeed @var{n}
1804 Set the output speed to @var{n}.
1808 Tell the tty kernel driver that the terminal has @var{n} rows. Non-POSIX.
1811 @itemx columns @var{n}
1814 Tell the kernel that the terminal has @var{n} columns. Non-POSIX.
1820 Print the number of rows and columns that the kernel thinks the
1821 terminal has. (Systems that don't support rows and columns in the kernel
1822 typically use the environment variables @env{LINES} and @env{COLUMNS}
1823 instead; however, GNU @code{stty} does not know anything about them.)
1828 Use line discipline @var{n}. Non-POSIX.
1832 Print the terminal speed.
1835 @cindex baud rate, setting
1836 @c FIXME: Is this still true that the baud rate can't be set
1837 @c higher than 38400?
1838 Set the input and output speeds to @var{n}. @var{n} can be one
1839 of: 0 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600
1840 19200 38400 @code{exta} @code{extb}. @code{exta} is the same as
1841 19200; @code{extb} is the same as 38400. 0 hangs up the line if
1842 @code{-clocal} is set.
1846 @node printenv invocation
1847 @section @code{printenv}: Print all or some environment variables
1850 @cindex printing all or some environment variables
1851 @cindex environment variables, printing
1853 @code{printenv} prints environment variable values. Synopsis:
1856 printenv [@var{option}] [@var{variable}]@dots{}
1859 If no @var{variable}s are specified, @code{printenv} prints the value of
1860 every environment variable. Otherwise, it prints the value of each
1861 @var{variable} that is set, and nothing for those that are not set.
1863 The only options are a lone @samp{--help} or @samp{--version}.
1864 @xref{Common options}.
1866 @cindex exit status of @code{printenv}
1870 0 if all variables specified were found
1871 1 if at least one specified variable was not found
1872 2 if a write error occurred
1876 @node tty invocation
1877 @section @code{tty}: Print file name of terminal on standard input
1880 @cindex print terminal file name
1881 @cindex terminal file name, printing
1883 @code{tty} prints the file name of the terminal connected to its standard
1884 input. It prints @samp{not a tty} if standard input is not a terminal.
1888 tty [@var{option}]@dots{}
1891 The program accepts the following option. Also see @ref{Common options}.
1901 Print nothing; only return an exit status.
1905 @cindex exit status of @code{tty}
1909 0 if standard input is a terminal
1910 1 if standard input is not a terminal
1911 2 if given incorrect arguments
1912 3 if a write error occurs
1916 @node User information
1917 @chapter User information
1919 @cindex user information, commands for
1920 @cindex commands for printing user information
1922 This section describes commands that print user-related information:
1923 logins, groups, and so forth.
1926 * id invocation:: Print real and effective uid and gid.
1927 * logname invocation:: Print current login name.
1928 * whoami invocation:: Print effective user id.
1929 * groups invocation:: Print group names a user is in.
1930 * users invocation:: Print login names of users currently logged in.
1931 * who invocation:: Print who is currently logged in.
1936 @section @code{id}: Print real and effective uid and gid
1939 @cindex real uid and gid, printing
1940 @cindex effective uid and gid, printing
1941 @cindex printing real and effective uid and gid
1943 @code{id} prints information about the given user, or the process
1944 running it if no user is specified. Synopsis:
1947 id [@var{option}]@dots{} [@var{username}]
1950 By default, it prints the real user id, real group id, effective user id
1951 if different from the real user id, effective group id if different from
1952 the real group id, and supplemental group ids.
1954 Each of these numeric values is preceded by an identifying string and
1955 followed by the corresponding user or group name in parentheses.
1957 The options cause @code{id} to print only part of the above information.
1958 Also see @ref{Common options}.
1965 Print only the group id.
1971 Print only the supplementary groups.
1977 Print the user or group name instead of the ID number. Requires
1978 @code{-u}, @code{-g}, or @code{-G}.
1984 Print the real, instead of effective, user or group id. Requires
1985 @code{-u}, @code{-g}, or @code{-G}.
1991 Print only the user id.
1996 @node logname invocation
1997 @section @code{logname}: Print current login name
2000 @cindex printing user's login name
2001 @cindex login name, printing
2002 @cindex user name, printing
2007 @code{logname} prints the calling user's name, as found in the file
2008 @file{/etc/utmp}, and exits with a status of 0. If there is no
2009 @file{/etc/utmp} entry for the calling process, @code{logname} prints
2010 an error message and exits with a status of 1.
2012 The only options are @samp{--help} and @samp{--version}. @xref{Common
2016 @node whoami invocation
2017 @section @code{whoami}: Print effective user id
2020 @cindex effective UID, printing
2021 @cindex printing the effective UID
2023 @code{whoami} prints the user name associated with the current
2024 effective user id. It is equivalent to the command @samp{id -un}.
2026 The only options are @samp{--help} and @samp{--version}. @xref{Common
2030 @node groups invocation
2031 @section @code{groups}: Print group names a user is in
2034 @cindex printing groups a user is in
2035 @cindex supplementary groups, printing
2037 @code{groups} prints the names of the primary and any supplementary
2038 groups for each given @var{username}, or the current process if no names
2039 are given. If names are given, the name of each user is printed before
2040 the list of that user's groups. Synopsis:
2043 groups [@var{username}]@dots{}
2046 The group lists are equivalent to the output of the command @samp{id -Gn}.
2048 The only options are @samp{--help} and @samp{--version}. @xref{Common
2052 @node users invocation
2053 @section @code{users}: Print login names of users currently logged in
2056 @cindex printing current usernames
2057 @cindex usernames, printing current
2059 @cindex login sessions, printing users with
2060 @code{users} prints on a single line a blank-separated list of user
2061 names of users currently logged in to the current host. Each user name
2062 corresponds to a login session, so if a user has more than one login
2063 session, that user's name will appear the same number of times in the
2072 With no @var{file} argument, @code{users} extracts its information from
2073 the file @file{/etc/utmp}. If a file argument is given, @code{users}
2074 uses that file instead. A common choice is @file{/etc/wtmp}.
2076 The only options are @samp{--help} and @samp{--version}. @xref{Common
2080 @node who invocation
2081 @section @code{who}: Print who is currently logged in
2084 @cindex printing current user information
2085 @cindex information, about current users
2087 @code{who} prints information about users who are currently logged on.
2091 @code{who} [@var{option}] [@var{file}] [am i]
2094 @cindex terminal lines, currently used
2096 @cindex remote hostname
2097 If given no non-option arguments, @code{who} prints the following
2098 information for each user currently logged on: login name, terminal
2099 line, login time, and remote hostname or X display.
2103 If given one non-option argument, @code{who} uses that instead of
2104 @file{/etc/utmp} as the name of the file containing the record of
2105 users logged on. @file{/etc/wtmp} is commonly given as an argument
2106 to @code{who} to look at who has previously logged on.
2110 If given two non-option arguments, @code{who} prints only the entry
2111 for the user running it (determined from its standard input), preceded
2112 by the hostname. Traditionally, the two arguments given are @samp{am
2113 i}, as in @samp{who am i}.
2115 The program accepts the following options. Also see @ref{Common options}.
2120 Same as @samp{who am i}.
2126 Print only the login names and the number of users logged on.
2127 Overrides all other options.
2131 Ignored; for compatibility with other versions of @code{who}.
2140 After the login time, print the number of hours and minutes that the
2141 user has been idle. @samp{.} means the user was active in last minute.
2142 @samp{old} means the user was idle for more than 24 hours.
2148 Attempt to canonicalize hostnames found in utmp through a DNS lookup. This
2149 is not the default because it can cause significant delays on systems with
2150 automatic dial-up internet access.
2156 Print a line of column headings.
2168 @cindex message status
2169 @pindex write@r{, allowed}
2170 After each login name print a character indicating the user's message status:
2173 @samp{+} allowing @code{write} messages
2174 @samp{-} disallowing @code{write} messages
2175 @samp{?} cannot find terminal device
2181 @node System context
2182 @chapter System context
2184 @cindex system context
2185 @cindex context, system
2186 @cindex commands for system context
2188 This section describes commands that print or change system-wide
2192 * date invocation:: Print or set system date and time.
2193 * uname invocation:: Print system information.
2194 * hostname invocation:: Print or set system name.
2195 * hostid invocation:: Print numeric host identifier.
2199 @node date invocation
2200 @section @code{date}: Print or set system date and time
2203 @cindex time, printing or setting
2204 @cindex printing the current time
2209 date [@var{option}]@dots{} [+@var{format}]
2210 date [-u|--utc|--universal] @c this avoids a newline in the output
2211 [ MMDDhhmm[[CC]YY][.ss] ]
2214 Invoking @code{date} with no @var{format} argument is equivalent to invoking
2215 @samp{date '+%a %b %e %H:%M:%S %Z %Y'}.
2217 @findex strftime @r{and @code{date}}
2218 @cindex time formats
2219 @cindex formatting times
2220 If given an argument that starts with a @samp{+}, @code{date} prints the
2221 current time and date (or the time and date specified by the
2222 @code{--date} option, see below) in the format defined by that argument,
2223 which is the same as in the @code{strftime} function. Except for
2224 directives, which start with @samp{%}, characters in the format string
2225 are printed unchanged. The directives are described below.
2228 * Time directives:: %[HIklMprsSTXzZ]
2229 * Date directives:: %[aAbBcdDhjmUwWxyY]
2230 * Literal directives:: %[%nt]
2231 * Padding:: Pad with zeroes, spaces (%_), or nothing (%-).
2232 * Setting the time:: Changing the system clock.
2233 * Options for date:: Instead of the current time.
2234 * Examples of date:: Examples.
2237 @node Time directives
2238 @subsection Time directives
2240 @cindex time directives
2241 @cindex directives, time
2243 @code{date} directives related to times.
2255 minute (00@dots{}59)
2259 time, 12-hour (hh:mm:ss [AP]M)
2261 @cindex epoch, seconds since
2262 @cindex seconds since the epoch
2263 @cindex beginning of time
2264 seconds since the epoch, i.e., 1 January 1970 00:00:00 UTC (a
2266 Note that this value is the number of seconds between the epoch
2267 and the current date as defined by the localtime system call.
2268 It isn't changed by the @samp{--date} option.
2270 second (00@dots{}60)
2272 time, 24-hour (hh:mm:ss)
2274 locale's time representation (%H:%M:%S)
2276 RFC-822 style numeric time zone (e.g., -0600 or +0100), or nothing if no
2277 time zone is determinable. This value reflects the @emph{current} time
2278 zone. It isn't changed by the @samp{--date} option.
2280 time zone (e.g., EDT), or nothing if no timezone is
2282 Note that this value reflects the @emph{current} time zone.
2283 It isn't changed by the @samp{--date} option.
2287 @node Date directives
2288 @subsection Date directives
2290 @cindex date directives
2291 @cindex directives, date
2293 @code{date} directives related to dates.
2297 locale's abbreviated weekday name (Sun@dots{}Sat)
2299 locale's full weekday name, variable length (Sunday@dots{}Saturday)
2301 locale's abbreviated month name (Jan@dots{}Dec)
2303 locale's full month name, variable length (January@dots{}December)
2305 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
2307 century (year divided by 100 and truncated to an integer) (00@dots{}99)
2309 day of month (01@dots{}31)
2315 day of year (001@dots{}366)
2319 week number of year with Sunday as first day of week (00@dots{}53).
2320 Days in a new year preceding the first Sunday are in week zero.
2322 week number of year with Monday as first day of the week as a decimal
2323 (01@dots{}53). If the week containing January 1 has four or more days in
2324 the new year, then it is considered week 1; otherwise, it is week 53 of
2325 the previous year, and the next week is week 1. (See the ISO 8601: 1988
2328 day of week (0@dots{}6) with 0 corresponding to Sunday
2330 week number of year with Monday as first day of week (00@dots{}53).
2331 Days in a new year preceding the first Monday are in week zero.
2333 locale's date representation (mm/dd/yy)
2335 last two digits of year (00@dots{}99)
2341 @node Literal directives
2342 @subsection Literal directives
2344 @cindex literal directives
2345 @cindex directives, literal
2347 @code{date} directives that produce literal strings.
2362 @cindex numeric field padding
2363 @cindex padding of numeric fields
2364 @cindex fields, padding numeric
2366 By default, @code{date} pads numeric fields with zeroes, so that, for
2367 example, numeric months are always output as two digits. GNU @code{date}
2368 recognizes the following numeric modifiers between the @samp{%} and the
2373 (hyphen) do not pad the field; useful if the output is intended for
2376 (underscore) pad the field with spaces; useful if you need a fixed
2377 number of characters in the output, but zeroes are too distracting.
2381 These are GNU extensions.
2383 Here is an example illustrating the differences:
2386 date +%d/%m -d "Feb 1"
2388 date +%-d/%-m -d "Feb 1"
2390 date +%_d/%_m -d "Feb 1"
2395 @node Setting the time
2396 @subsection Setting the time
2398 @cindex setting the time
2399 @cindex time setting
2400 @cindex appropriate privileges
2402 If given an argument that does not start with @samp{+}, @code{date} sets
2403 the system clock to the time and date specified by that argument (as
2404 described below). You must have appropriate privileges to set the
2405 system clock. The @samp{--date} and @samp{--set} options may not be
2406 used with such an argument. The @samp{--universal} option may be used
2407 with such an argument to indicate that the specified time and date are
2408 relative to Coordinated Universal Time rather than to the local time
2411 The argument must consist entirely of digits, which have the following
2424 first two digits of year (optional)
2426 last two digits of year (optional)
2431 The @samp{--set} option also sets the system clock; see the next section.
2434 @node Options for date
2435 @subsection Options for @code{date}
2437 @cindex @code{date} options
2438 @cindex options for @code{date}
2440 The program accepts the following options. Also see @ref{Common options}.
2444 @item -d @var{datestr}
2445 @itemx --date=@var{datestr}
2448 @cindex parsing date strings
2449 @cindex date strings, parsing
2450 @cindex arbitrary date strings, parsing
2453 @opindex next @var{day}
2454 @opindex last @var{day}
2455 Display the time and date specified in @var{datestr} instead of the
2456 current time and date. @var{datestr} can be in almost any common
2457 format. It can contain month names, timezones, @samp{am} and @samp{pm},
2458 @samp{yesterday}, @samp{ago}, @samp{next}, etc. @xref{Date input formats}.
2460 @item -f @var{datefile}
2461 @itemx --file=@var{datefile}
2464 Parse each line in @var{datefile} as with @samp{-d} and display the
2465 resulting time and date. If @var{datefile} is @samp{-}, use standard
2466 input. This is useful when you have many dates to process, because the
2467 system overhead of starting up the @code{date} executable many times can
2470 @item -I[@var{timespec}]
2471 @itemx --iso-8601[=@var{timespec}]
2472 @opindex -I[@var{timespec}]
2473 @opindex --iso-8601[=@var{timespec}]
2474 Display the date using the ISO 8601 format, @samp{%Y-%m-%d}.
2476 The optional argument @var{timespec} specifies the number of additional
2477 terms of the time to include. It can be one of the following:
2480 The default behavior: print just the date.
2483 Append the hour of the day to the date.
2486 Append the hours and minutes.
2489 Append the hours, minutes, and seconds.
2492 If showing any time terms, then include the time zone using the format
2499 Display the time and date using the RFC-822-conforming
2500 format, @samp{%a, %_d %b %Y %H:%M:%S %z}.
2503 @itemx --reference=@var{file}
2505 @opindex --reference
2506 Display the time and date reference according to the last modification
2507 time of @var{file}, instead of the current time and date.
2509 @item -s @var{datestr}
2510 @itemx --set=@var{datestr}
2513 Set the time and date to @var{datestr}. See @samp{-d} above.
2520 @opindex --universal
2521 @cindex Coordinated Universal Time
2523 @cindex Greenwich Mean Time
2525 Use Coordinated Universal Time (@sc{utc}) by operating as if the
2526 @env{TZ} environment variable was set to the string @samp{UTC0}.
2527 Normally, @command{date} operates in the time zone indicated by
2528 @env{TZ}, or the system default if @env{TZ} is not set. Coordinated
2529 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
2534 @node Examples of date
2535 @subsection Examples of @code{date}
2537 @cindex examples of @code{date}
2539 Here are a few examples. Also see the documentation for the @samp{-d}
2540 option in the previous section.
2545 To print the date of the day before yesterday:
2548 date --date='2 days ago'
2552 To print the date of the day three months and one day hence:
2554 date --date='3 months 1 day'
2558 To print the day of year of Christmas in the current year:
2560 date --date='25 Dec' +%j
2564 To print the current full month name and the day of the month:
2569 But this may not be what you want because for the first nine days of
2570 the month, the @samp{%d} expands to a zero-padded two-digit field,
2571 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
2574 To print a date without the leading zero for one-digit days
2575 of the month, you can use the (GNU extension) @code{-} modifier to suppress
2576 the padding altogether.
2578 date -d 1may '+%B %-d'
2582 To print the current date and time in the format required by many
2583 non-GNU versions of @code{date} when setting the system clock:
2589 To set the system clock forward by two minutes:
2591 date --set='+2 minutes'
2595 To print the date in the format specified by RFC-822,
2596 use @samp{date --rfc}. I just did and saw this:
2599 Mon, 25 Mar 1996 23:34:17 -0600
2603 To convert a date string to the number of seconds since the epoch
2604 (which is 1970-01-01 00:00:00 UTC), use the @samp{--date} option with
2605 the @samp{%s} format. That can be useful in sorting and/or graphing
2606 and/or comparing data by date. The following command outputs the
2607 number of the seconds since the epoch for the time two minutes after the
2611 date --date='1970-01-01 00:02:00 +0000' +%s
2615 If you do not specify time zone information in the date string,
2616 @command{date} uses your computer's idea of the time zone when
2617 interpreting the string. For example, if your computer's time zone is
2618 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
2619 seconds) behind UTC:
2622 # local time zone used
2623 date --date='1970-01-01 00:02:00' +%s
2628 If you're sorting or graphing dated data, your raw date values may be
2629 represented as seconds since the epoch. But few people can look at
2630 the date @samp{946684800} and casually note ``Oh, that's the first second
2631 of the year 2000 in Greenwich, England.''
2634 date --date='2000-01-01 UTC' +%s
2638 To convert such an unwieldy number of seconds back to
2639 a more readable form, use a command like this:
2642 # local time zone used
2643 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
2644 1999-12-31 19:00:00 -0500
2650 @node uname invocation
2651 @section @code{uname}: Print system information
2654 @cindex print system information
2655 @cindex system information, printing
2657 @code{uname} prints information about the machine and operating system
2658 it is run on. If no options are given, @code{uname} acts as if the
2659 @code{-s} option were given. Synopsis:
2662 uname [@var{option}]@dots{}
2665 If multiple options or @code{-a} are given, the selected information is
2666 printed in this order:
2669 @var{sysname} @var{nodename} @var{release} @var{osversion} @var{machine}
2672 The @var{osversion}, at least, may well be multiple words. For example:
2676 @result{} Linux hayley 1.0.4 #3 Thu May 12 18:06:34 1994 i486
2680 The program accepts the following options. Also see @ref{Common options}.
2688 Print all of the below information.
2694 @cindex machine type
2695 @cindex hardware type
2696 Print the machine (hardware) type.
2704 @cindex network node name
2705 Print the machine's network node hostname.
2710 @opindex --processor
2711 @cindex host processor type
2712 Print the machine's processor type
2718 @cindex operating system release
2719 @cindex release of operating system
2720 Print the operating system release.
2726 @cindex operating system name
2727 @cindex name of operating system
2728 Print the operating system name.
2732 @cindex operating system version
2733 @cindex version of operating system
2734 Print the operating system version.
2738 @node hostname invocation
2739 @section @code{hostname}: Print or set system name
2742 @cindex setting the hostname
2743 @cindex printing the hostname
2744 @cindex system name, printing
2745 @cindex appropriate privileges
2747 With no arguments, @code{hostname} prints the name of the current host
2748 system. With one argument, it sets the current host name to the
2749 specified string. You must have appropriate privileges to set the host
2753 hostname [@var{name}]
2756 The only options are @samp{--help} and @samp{--version}. @xref{Common
2760 @node hostid invocation
2761 @section @code{hostid}: Print numeric host identifier.
2764 @cindex printing the host identifier
2766 @code{hostid} prints the numeric identifier of the current host
2767 in hexadecimal. This command accepts no arguments.
2768 The only options are @samp{--help} and @samp{--version}.
2769 @xref{Common options}.
2771 For example, here's what it prints on one system I use:
2778 On that system, the 32-bit quantity happens to be closely
2779 related to the system's Internet address, but that isn't always
2783 @node Modified command invocation
2784 @chapter Modified command invocation
2786 @cindex modified command invocation
2787 @cindex invocation of commands, modified
2788 @cindex commands for invoking other commands
2790 This section describes commands that run other commands in some context
2791 different than the current one: a modified environment, as a different
2795 * chroot invocation:: Modify the root directory.
2796 * env invocation:: Modify environment variables.
2797 * nice invocation:: Modify scheduling priority.
2798 * nohup invocation:: Immunize to hangups.
2799 * su invocation:: Modify user and group id.
2803 @node chroot invocation
2804 @section @code{chroot}: Run a command with a different root directory
2807 @cindex running a program in a specified root directory
2808 @cindex root directory, running a program in a specified
2810 @code{chroot} runs a command with a specified root directory.
2811 On many systems, only the super-user can do this.
2815 chroot @var{newroot} [@var{command} [@var{args}]@dots{}]
2819 Ordinarily, filenames are looked up starting at the root of the
2820 directory structure, i.e., @file{/}. @code{chroot} changes the root to
2821 the directory @var{newroot} (which must exist) and then runs
2822 @var{command} with optional @var{args}. If @var{command} is not
2823 specified, the default is the value of the @env{SHELL} environment
2824 variable or @code{/bin/sh} if not set, invoked with the @samp{-i} option.
2826 The only options are @samp{--help} and @samp{--version}. @xref{Common
2829 Here are a few tips to help avoid common problems in using chroot.
2830 To start with a simple example, make @var{command} refer to a statically
2831 linked binary. If you were to use a dynamically linked executable, then
2832 you'd have to arrange to have the shared libraries in the right place under
2833 your new root directory.
2835 For example, if you create a statically linked `ls' executable,
2836 and put it in /tmp/empty, you can run this command as root:
2839 $ chroot /tmp/empty /ls -Rl /
2842 Then you'll see output like this:
2847 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
2850 If you want to use a dynamically linked executable, say @code{bash},
2851 then first run @samp{ldd bash} to see what shared objects it needs.
2852 Then, in addition to copying the actual binary, also copy the listed
2853 files to the required positions under your intended new root directory.
2854 Finally, if the executable requires any other files (e.g., data, state,
2855 device files), copy them into place, too.
2858 @node env invocation
2859 @section @code{env}: Run a command in a modified environment
2862 @cindex environment, running a program in a modified
2863 @cindex modified environment, running a program in a
2864 @cindex running a program in a modified environment
2866 @code{env} runs a command with a modified environment. Synopses:
2869 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
2870 [@var{command} [@var{args}]@dots{}]
2874 Arguments of the form @samp{@var{variable}=@var{value}} set
2875 the environment variable @var{variable} to value @var{value}.
2876 @var{value} may be empty (@samp{@var{variable}=}). Setting a variable
2877 to an empty value is different from unsetting it.
2880 The first remaining argument specifies the program name to invoke; it is
2881 searched for according to the @env{PATH} environment variable. Any
2882 remaining arguments are passed as arguments to that program.
2884 @cindex environment, printing
2886 If no command name is specified following the environment
2887 specifications, the resulting environment is printed. This is like
2888 specifying a command name of @code{printenv}.
2890 The program accepts the following options. Also see @ref{Common options}.
2895 @itemx --unset=@var{name}
2898 Remove variable @var{name} from the environment, if it was in the
2903 @itemx --ignore-environment
2906 @opindex --ignore-environment
2907 Start with an empty environment, ignoring the inherited environment.
2912 @node nice invocation
2913 @section @code{nice}: Run a command with modified scheduling priority
2916 @cindex modifying scheduling priority
2917 @cindex scheduling priority, modifying
2918 @cindex priority, modifying
2919 @cindex appropriate privileges
2921 @code{nice} prints or modifies the scheduling priority of a job.
2925 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
2928 If no arguments are given, @code{nice} prints the current scheduling
2929 priority, which it inherited. Otherwise, @code{nice} runs the given
2930 @var{command} with its scheduling priority adjusted. If no
2931 @var{adjustment} is given, the priority of the command is incremented by
2932 10. You must have appropriate privileges to specify a negative
2933 adjustment. The priority can be adjusted by @code{nice} over the range
2934 of -20 (the highest priority) to 19 (the lowest).
2936 @cindex conflicts with shell built-ins
2937 @cindex built-in shell commands, conflicts with
2938 Because most shells have a built-in command by the same name, using the
2939 unadorned command name in a script or interactively may get you
2940 different functionality than that described here.
2942 The program accepts the following option. Also see @ref{Common options}.
2945 @item -n @var{adjustment}
2946 @itemx -@var{adjustment}
2947 @itemx --adjustment=@var{adjustment}
2949 @opindex --adjustment
2950 @opindex -@var{adjustment}
2951 Add @var{adjustment} instead of 10 to the command's priority.
2955 @node nohup invocation
2956 @section @code{nohup}: Run a command immune to hangups
2959 @cindex hangups, immunity to
2960 @cindex immunity to hangups
2961 @cindex logging out and continuing to run
2964 @code{nohup} runs the given @var{command} with hangup signals ignored,
2965 so that the command can continue running in the background after you log
2969 nohup @var{command} [@var{arg}]@dots{}
2973 @code{nohup} increases the scheduling priority of @var{command} by 5, so
2974 it has a slightly smaller chance to run. If standard output is a terminal,
2975 it and standard error are redirected so that they are appended to the
2976 file @file{nohup.out}; if that cannot be written to, they are appended
2977 to the file @file{$HOME/nohup.out}. If that cannot be written to, the
2980 If @code{nohup} creates either @file{nohup.out} or
2981 @file{$HOME/nohup.out}, it creates it with no ``group'' or ``other''
2982 access permissions. It does not change the permissions if the output
2983 file already existed.
2985 @code{nohup} does not automatically put the command it runs in the
2986 background; you must do that explicitly, by ending the command line
2989 The only options are @samp{--help} and @samp{--version}. @xref{Common
2994 @section @code{su}: Run a command with substitute user and group id
2997 @cindex substitute user and group ids
2998 @cindex user id, switching
2999 @cindex super-user, becoming
3000 @cindex root, becoming
3002 @code{su} allows one user to temporarily become another user. It runs a
3003 command (often an interactive shell) with the real and effective user
3004 id, group id, and supplemental groups of a given @var{user}. Synopsis:
3007 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
3010 @cindex passwd entry, and @code{su} shell
3012 @flindex /etc/passwd
3013 If no @var{user} is given, the default is @code{root}, the super-user.
3014 The shell to use is taken from @var{user}'s @code{passwd} entry, or
3015 @file{/bin/sh} if none is specified there. If @var{user} has a
3016 password, @code{su} prompts for the password unless run by a user with
3017 effective user id of zero (the super-user).
3024 By default, @code{su} does not change the current directory.
3025 It sets the environment variables @env{HOME} and @env{SHELL}
3026 from the password entry for @var{user}, and if @var{user} is not
3027 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
3028 By default, the shell is not a login shell.
3030 Any additional @var{arg}s are passed as additional arguments to the
3034 GNU @code{su} does not treat @file{/bin/sh} or any other shells specially
3035 (e.g., by setting @code{argv[0]} to @samp{-su}, passing @code{-c} only
3036 to certain shells, etc.).
3039 @code{su} can optionally be compiled to use @code{syslog} to report
3040 failed, and optionally successful, @code{su} attempts. (If the system
3041 supports @code{syslog}.) However, GNU @code{su} does not check if the
3042 user is a member of the @code{wheel} group; see below.
3044 The program accepts the following options. Also see @ref{Common options}.
3047 @item -c @var{command}
3048 @itemx --command=@var{command}
3051 Pass @var{command}, a single command line to run, to the shell with
3052 a @code{-c} option instead of starting an interactive shell.
3059 @cindex file name pattern expansion, disabled
3060 @cindex globbing, disabled
3061 Pass the @code{-f} option to the shell. This probably only makes sense
3062 if the shell run is @code{csh} or @code{tcsh}, for which the @code{-f}
3063 option prevents reading the startup file (@file{.cshrc}). With
3064 Bourne-like shells, the @code{-f} option disables file name pattern
3065 expansion (globbing), which is not likely to be useful.
3073 @c other variables already indexed above
3076 @cindex login shell, creating
3077 Make the shell a login shell. This means the following. Unset all
3078 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
3079 (which are set as described above), and @env{USER} and @env{LOGNAME}
3080 (which are set, even for the super-user, as described above), and set
3081 @env{PATH} to a compiled-in default value. Change to @var{user}'s home
3082 directory. Prepend @samp{-} to the shell's name, intended to make it
3083 read its login startup file(s).
3087 @itemx --preserve-environment
3090 @opindex --preserve-environment
3091 @cindex environment, preserving
3092 @flindex /etc/shells
3093 @cindex restricted shell
3094 Do not change the environment variables @env{HOME}, @env{USER},
3095 @env{LOGNAME}, or @env{SHELL}. Run the shell given in the environment
3096 variable @env{SHELL} instead of the shell from @var{user}'s passwd
3097 entry, unless the user running @code{su} is not the superuser and
3098 @var{user}'s shell is restricted. A @dfn{restricted shell} is one that
3099 is not listed in the file @file{/etc/shells}, or in a compiled-in list
3100 if that file does not exist. Parts of what this option does can be
3101 overridden by @code{--login} and @code{--shell}.
3103 @item -s @var{shell}
3104 @itemx --shell=@var{shell}
3107 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
3108 unless the user running @code{su} is not the superuser and @var{user}'s
3109 shell is restricted (see @samp{-m} just above).
3113 @cindex wheel group, not supported
3114 @cindex group wheel, not supported
3116 @heading Why GNU @code{su} does not support the @samp{wheel} group
3118 (This section is by Richard Stallman.)
3122 Sometimes a few of the users try to hold total power over all the
3123 rest. For example, in 1984, a few users at the MIT AI lab decided to
3124 seize power by changing the operator password on the Twenex system and
3125 keeping it secret from everyone else. (I was able to thwart this coup
3126 and give power back to the users by patching the kernel, but I
3127 wouldn't know how to do that in Unix.)
3129 However, occasionally the rulers do tell someone. Under the usual
3130 @code{su} mechanism, once someone learns the root password who
3131 sympathizes with the ordinary users, he or she can tell the rest. The
3132 ``wheel group'' feature would make this impossible, and thus cement the
3133 power of the rulers.
3135 I'm on the side of the masses, not that of the rulers. If you are
3136 used to supporting the bosses and sysadmins in whatever they do, you
3137 might find this idea strange at first.
3143 @cindex delaying commands
3144 @cindex commands for delaying
3146 @c Perhaps @code{wait} or other commands should be described here also?
3149 * sleep invocation:: Delay for a specified time.
3153 @node sleep invocation
3154 @section @code{sleep}: Delay for a specified time
3157 @cindex delay for a specified time
3159 @code{sleep} pauses for an amount of time specified by the sum of
3160 the values of the command line arguments.
3164 sleep @var{number}[smhd]@dots{}
3168 Each argument is a number followed by an optional unit; the default
3169 is seconds. The units are:
3182 Historical implementations of @code{sleep} have required that
3183 @var{number} be an integer. However, GNU @code{sleep} accepts
3184 arbitrary floating point numbers.
3186 The only options are @samp{--help} and @samp{--version}. @xref{Common
3190 @node Numeric operations
3191 @chapter Numeric operations
3193 @cindex numeric operations
3194 These programs do numerically-related operations.
3197 * factor invocation:: Show factors of numbers.
3198 * seq invocation:: Print sequences of numbers.
3202 @node factor invocation
3203 @section @code{factor}: Print prime factors
3206 @cindex prime factors
3208 @code{factor} prints prime factors. Synopses:
3211 factor [@var{number}]@dots{}
3215 If no @var{number} is specified on the command line, @code{factor} reads
3216 numbers from standard input, delimited by newlines, tabs, or spaces.
3218 The only options are @samp{--help} and @samp{--version}. @xref{Common
3221 The algorithm it uses is not very sophisticated, so for some inputs
3222 @code{factor} runs for a long time. The hardest numbers to factor are
3223 the products of large primes. Factoring the product of the two largest 32-bit
3224 prime numbers takes over 10 minutes of CPU time on a 400MHz Pentium II.
3227 $ p=`echo '4294967279 * 4294967291'|bc`
3229 18446743979220271189: 4294967279 4294967291
3232 In contrast, @code{factor} factors the largest 64-bit number in just
3233 over a tenth of a second:
3236 $ factor `echo '2^64-1'|bc`
3237 18446744073709551615: 3 5 17 257 641 65537 6700417
3240 @node seq invocation
3241 @section @code{seq}: Print numeric sequences
3244 @cindex numeric sequences
3245 @cindex sequence of numbers
3247 @code{seq} prints a sequence of numbers to standard output. Synopses:
3250 seq [@var{option}]@dots{} [@var{first} [@var{increment}]] @var{last}@dots{}
3253 @code{seq} prints the numbers from @var{first} to @var{last} by
3254 @var{increment}. By default, @var{first} and @var{increment} are both 1,
3255 and each number is printed on its own line. All numbers can be reals,
3258 The program accepts the following options. Also see @ref{Common options}.
3261 @item -f @var{format}
3262 @itemx --format=@var{format}
3263 @opindex -f @var{format}
3264 @opindex --format=@var{format}
3265 @cindex formatting of numbers in @code{seq}
3266 Print all numbers using @var{format}; default @samp{%g}.
3267 @var{format} must contain exactly one of the floating point
3268 output formats @samp{%e}, @samp{%f}, or @samp{%g}.
3270 @item -s @var{string}
3271 @itemx --separator=@var{string}
3272 @cindex separator for numbers in @code{seq}
3273 Separate numbers with @var{string}; default is a newline.
3274 The output always terminates with a newline.
3277 @itemx --equal-width
3278 Print all numbers with the same width, by padding with leading zeroes.
3279 (To have other kinds of padding, use @samp{--format}).
3283 If you want to use @code{seq} to print sequences of large integer values,
3284 don't use the default @samp{%g} format since it can result in
3288 $ seq 1000000 1000001
3293 Instead, you can use the format, @samp{%1.f},
3294 to print large decimal numbers with no exponent and no decimal point.
3297 $ seq --format=%1.f 1000000 1000001
3302 If you want hexadecimal output, you can use @code{printf}
3303 to perform the conversion:
3306 $ printf %x'\n' `seq -f %1.f 1048575 1024 1050623`
3312 For very long lists of numbers, use xargs to avoid
3313 system limitations on the length of an argument list:
3316 $ seq -f %1.f 1000000 | xargs printf %x'\n' |tail -3
3322 To generate octal output, use the printf @code{%o} format instead
3323 of @code{%x}. Note however that using printf works only for numbers
3324 smaller than @code{2^32}:
3327 $ printf "%x\n" `seq -f %1.f 4294967295 4294967296`
3329 bash: printf: 4294967296: Numerical result out of range
3332 On most systems, seq can produce whole-number output for values up to
3333 @code{2^53}, so here's a more general approach to base conversion that
3334 also happens to be more robust for such large numbers. It works by
3335 using @code{bc} and setting its output radix variable, @var{obase},
3336 to @samp{16} in this case to produce hexadecimal output.
3339 $ (echo obase=16; seq -f %1.f 4294967295 4294967296)|bc
3344 Be careful when using @code{seq} with a fractional @var{increment},
3345 otherwise you may see surprising results. Most people would expect to
3346 see @code{0.3} printed as the last number in this example:
3353 But that doesn't happen on most systems because @code{seq} is
3354 implemented using binary floating point arithmetic (via the C
3355 @code{double} type) -- which means some decimal numbers like @code{.1}
3356 cannot be represented exactly. That in turn means some nonintuitive
3357 conditions like @code{.1 * 3 > .3} will end up being true.
3359 To work around that in the above example, use a slightly larger number as
3360 the @var{last} value:
3363 $ seq -s' ' 0 .1 .31
3367 In general, when using an @var{increment} with a fractional part, where
3368 (@var{last} - @var{first}) / @var{increment} is (mathematically) a whole
3369 number, specify a slightly larger (or smaller, if @var{increment} is negative)
3370 value for @var{last} to ensure that @var{last} is the final value printed
3382 @c texinfo-column-for-description: 33