img symield

The K Shell Program Guide(1)

发表于2004/10/15 14:28:00  803人阅读

Wildcard Matches
? Any single character
* Any string of characters
[set] Any character in set
[!set] Any character not in set
Expression Matches
[abc] a, b, or c
[.,;] Period, comma, or semicolon
[-_] Dash and underscore
[a-c] a, b, or c
[a-z] All lowercase letters
[!0-9] All non-digits
[0-9!] All digits and exclamation point
[a-zA-Z] All lower- and uppercase letters
[a-zA-Z0-9_-] All letters, all digits, underscore, and dash
Utility Purpose
cat Copy input to output
grep Search for strings in the input
sort Sort lines in the input
cut Extract columns from input
sed Perform editing operations on input
tr Translate characters in the input to other characters
fc -l
fc -e - number
user's environment file is .profile.
6)Built-in Commands and Keywords

Here is a summary of all built-in commands and keywords.

Command Chapter Summary
: 7 Do nothing (just do expansions of arguments).
. 4 Read file and execute its contents in current shell.
alias 3 Set up shorthand for command or command line.
bg 8 Put job in background.
break 5

Exit from surrounding for, select, while, or until loop.

case 5 Multi-way conditional construct.
cd 1 Change working directory.

Skip to next iteration of for, select, while, or until loop.

echo 4 Expand and print arguments (obsolete).
exec 9 Replace shell with given program.
exit 5 Exit from shell.
export 3 Create environment variables.
eval 7 Process arguments as a command line.
fc 2 Fix command (edit history file).
fg 8 Put background job in foreground.
for 5 Looping construct.
function 4 Define function.
getopts 6 Process command-line options.
if 5 Conditional construct.
jobs 1 List background jobs.
kill 8 Send signal to process.
let 6 Arithmetic variable assignment.
newgrp Start new shell with new group ID.
print 1 Expand and print arguments on standard output.
pwd 1 Print working directory.
read 7 Read a line from standard input.
readonly 6 Make variables read-only (unassignable).
return 5 Return from surrounding function or script.
select 5 Menu generation construct.
set 3 Set options.
shift 6 Shift command-line arguments.
time Run command and print execution times.
trap 8 Set up signal-catching routine.
typeset 6 Set special characteristics of variables.
ulimit 10 Set/show process resource limits.
umask 10 Set/show file permission mask.
unalias 3 Remove alias definitions.
unset 3 Remove definitions of variables or functions.
until 5 Looping construct.
wait 8 Wait for background job(s) to finish.
whence 3 Identify source of command.
while 5 Looping construct.
Built-in Shell Variables

Variable Chapter Meaning
# 4 Number of arguments given to current process.
- Options given to shell on invocation.
? 5 Exit status of previous command.
$ 8 Process ID of shell process.
_ Last argument to previous command.
! 8 Process ID of last background command.
CDPATH 3 List of directories for cd command to search.

Width of display in columns (for editing modes and select).


Used to set editing mode; also used by mail and other programs.

ERRNO A Error number of last system call that failed.

Name of file to run as environment file when shell is invoked.

FCEDIT 2 Default editor for fc command.
FPATH 4 Search path for autoloaded functions.

Internal field separator: list of characters that act as word separators. Normally set to SPACE, TAB, and NEWLINE.

HISTFILE 2 Name of command history file.
HISTSIZE 2 Number of lines kept in history file.
HOME 3 Home (login) directory.
LINENO 9 Number of line in script or function that just ran.
LINES 3 Height of display in lines (for select command).
MAIL 3 Name of file to check for new mail.
MAILCHECK 3 How often (in seconds) to check for new mail.

List of file names to check for new mail, if MAIL is not set.

OLDPWD 3 Previous working directory.
OPTARG 6 Argument to option being processed by getopts.
OPTIND 6 Number of first argument after options.
PATH 3 Search path for commands.
PS1 3 Primary command prompt string.
PS2 3 Prompt string for line continuations.
PS3 5 Prompt string for select command.
PS4 9 Prompt string for xtrace option.
PPID 8 Process ID of parent process.
PWD 3 Current working directory.

Random number between 0 and 32767 (2215-1).


User's response to select command; result of read command if no variable names given.

SECONDS 3 Number of seconds since shell was invoked.
SHELL 3 Full pathname of shell.

If set to a positive integer, number of seconds between commands after which shell automatically terminates.

VISUAL 2 Used to set editing mode.

8)Test Operators

These are the operators that are used with the [[...]] construct. They can be logically combined with && ("and") and || ("or") and grouped with parenthesis.

Operator True If...
-a file file exists.
-b file file is a block device file.
-c file file is a character device file.
-d file file is a directory.
-f file file is a regular file.
-g file file has its setgid bit set.
-k file file has its sticky bit set.
-n string string is non-null.
-o option option is set.
-p file file is a pipe or named pipe (FIFO file).
-r file file is readable.
-s file file is not empty.
-t N File descriptor N points to a terminal.
-u file file has its setuid bit set.
-w file file is writeable.
-x file

file is executable, or file is a directory that can be searched.

-z string string is null.
-G file file's group ID is the same as that of the shell.
-L file file is a symbolic link.
-O file file is owned by the shell's user ID.
-S file file is a socket.
fileA -nt fileB fileA is newer than fileB.
fileA -ot fileB fileA is older than fileB.
fileA -ef fileB

fileA and fileB point to the same file.

string = pattern

string matches pattern (which can contain wildcards).

string != pattern string does not match pattern.
stringA < stringB

stringA comes before stringB in dictionary order.

stringA > stringB

stringA comes after stringB in dictionary order.

exprA -eq exprB

Arithmetic expressions exprA and exprB are equal.

exprA -ne exprB

Arithmetic expressions exprA and exprB are not equal.

exprA -lt exprB exprA is less than exprB.
exprA -gt exprB exprA is greater than exprB.
exprA -le exprB exprA is less than or equal to exprB.
exprA -ge exprB exprA is greater than or equal to exprB.

9)Typeset Options

These are arguments to the typeset command.

Option Meaning
With no option, create local variable within function.
-L Left justify and remove leading blanks.
-R Right justify and remove trailing blanks.
-f With no arguments, prints all function definitions.
-f fname Prints the definition of function fname.
+f Prints all function names.
-ft Turns on trace mode for named function(s).
+ft Turns off trace mode for named function(s).
-fu Defines given name(s) as autoloaded function(s).
-i Declare variable as an integer.
-l Convert all letters to lowercase.
-r Make variable read-only.
-u Convert all letters to uppercase.

Export variable, i.e., put in environment so that it is passed to subshells

Substitution Operators
Operator Substitution

If varname exists and isn't null, return its value; otherwise return word.


Returning a default value if the variable is undefined.


${count:-0} evaluates to 0 if count is undefined.


If varname exists and isn't null, return its value; otherwise set it to word and then return its value.[7]


Setting a variable to a default value if it is undefined.


${count:=0} sets count to 0 if it is undefined.


If varname exists and isn't null, return its value; otherwise print varname: followed by message, and abort the current command or script. Omitting message produces the default message parameter null or not set.


Catching errors that result from variables being undefined.


{count:?" undefined!" } prints "count: undefined!" and exits if count is undefined.


If varname exists and isn't null, return word; otherwise return null.


Testing for the existence of a variable.


${count:+1} returns 1 (which could mean "true") if count is defined.

0 0


取 消