3 sh, ., break, case, cd, continue, eval, exec, exit, export, for, if, read, readonly, set, shift, trap, umask, wait, while \- shell
5 \fBsh\fR [\fB\-eiknqstvxu\fR] [\fB\-c \fIstr\fR] \fB[\fIfile\fR]\fR
18 .FL "\-c" "Execute the commands in \fIstr\fR"
19 .FL "\-e" "Quit on error"
20 .FL "\-i" "Interactive mode; ignore QUIT, TERMINATE, INTERRUPT"
21 .FL "\-k" "Look for name=value everywhere on command line"
22 .FL "\-n" "Do not execute commands"
23 .FL "\-q" "Change qflag from sig_ign to sig_del"
24 .FL "\-s" "Read commands from standard input"
25 .FL "\-t" "Exit after reading and executing one command"
26 .FL "\-v" "Echo input lines as they are read"
28 .FL "\-u" "Unset variables"
30 .EX "sh script" "Run a shell script"
33 is the shell, which forms the user's main interface with the system.
34 On startup, the shell reads /etc/profile and $HOME/.profile, if they exist,
35 and executes any commands they contain. The Minix shell has most of the
36 features of the V7 (Bourne) shell, including redirection of input and output,
37 pipes, magic characters, background processes, and shell scripts. A brief
38 summary follows, but whole books have been written on shell programming alone.
40 Some of the more common notations are:
45 date # Regular command
47 sort <file # Redirect \fIstdin\fR (standard input)
49 sort <file1 >file2 # Redirect \fIstdin\fR and \fIstdout\fR
51 cc file.c 2>error # Redirect \fIstderr\fR
53 a.out >f 2>&1 # Combine standard output and standard error
55 sort <file1 >>file2 # Append output to \fIfile2\fR
57 sort <file1 >file2 & # Background job
59 (ls \-l; a.out) & # Run two background commands sequentially
61 sort <file | wc # Two-process pipeline
63 sort <f | uniq | wc # Three-process pipeline
65 ls \-l *.c # List all files ending in \fI.c\fR
67 ls \-l [\fIa-c\fR]* # List all files beginning with \fIa\fR, \fIb\fR, or \fIc\fR
69 ls \-l ? # List all one-character file names
71 ls \e? # List the file whose name is question mark
73 ls \(fm???\(fm # List the file whose name is three question marks
75 v=/usr/ast # Set shell variable \fIv\fR
77 ls \-l $v # Use shell variable \fIv\fR
79 PS1=\(fmHi! \(fm # Change the primary prompt to \fIHi!\fR
81 PS2=\(fmMore: \(fm # Change the secondary prompt to \fIMore:\fR
83 ls \-l $HOME # List the home directory
85 echo $PATH # Echo the search path
87 echo $? # Echo exit status of previous command in decimal
89 echo $$ # Echo shell's pid in decimal
91 echo $! # Echo PID of last background process
93 echo $# # Echo number of parameters (shell script)
95 echo $2 # Echo second parameter (shell script)
97 echo "$2" # Echo second parameter without expanding spaces
99 echo $* # Echo all parameters (shell script)
101 echo $@ # Echo all parameters (shell script)
103 echo "$@" # Echo all parameters without expanding spaces
106 The shell uses the following variables for specific purposes:
111 SHELL the path of the current shell
113 HOME the default value for the cd(1) command
115 PATH the directories to be searched to find commands
117 IFS the internal field separators for command strings
119 PS1 the primary shell prompt
121 PS2 the secondary shell prompt
124 There are various forms of substitution on the shell command line:
129 `...` Command string between back-quotes is replaced by its output
131 "..." Permits variable substitution between quotes
133 \&'...' Inhibits variable substitution between quotes
135 $VAR Replaced by contents of variable VAR
137 ${VAR} Delimits variable VAR from any following string
140 The expressions below depend on whether or not VAR has ever been set.
141 If VAR has been set, they give:
146 ${VAR-str} Replace expression by VAR, else by str
148 ${VAR=str} Replace expression by VAR, else by str and set VAR to str
150 ${VAR?str} Replace expression by VAR, else print str and exit shell
152 ${VAR+str} Replace expression by str, else by null string
155 If a colon is placed after VAR, the expressions depend on whether or not
156 VAR is currently set and non-null.
158 The shell has a number of built-in commands:
165 \&. fn execute shell script fn on current path
167 break [n] break from a for, until or while loop; exit n levels
169 continue [n] continue a for, until or while loop; resume nth loop
171 cd [dir] change current working directory; move to $HOME
173 eval cmd rescan cmd, performing substitutions
175 eval rescan the current command line
177 exec cmd execute cmd without creating a new process
179 exec <|> with no command name, modify shell I/O
181 exit [n] exit a shell program, with exit value n
183 export [var] export var to shell's children; list exported variables
185 pwd print the name of the current working directory
187 read var read a line from stdin and assign to var
189 readonly [var] make var readonly; list readonly variables
191 set -f set shell flag (+f unsets flag)
193 set str set positional parameter to str
195 set show the current shell variables
197 shift reassign positional parameters (except ${0}) one left
199 times print accumulated user and system times for processes
201 trap arg sigs trap signals sigs and run arg on receipt
203 trap list trapped signals
205 umask [n] set the user file creation mask; show the current umask
207 wait [n] wait for process pid n; wait for all processes
210 The shell also contains a programming language, which has the following
211 operators and flow control statements:
216 # Comment The rest of the line is ignored
218 = Assignment Set a shell variable
220 && Logical AND Execute second command only if first succeeds
222 || Logical OR Execute second command only if first fails
224 (...) Group Execute enclosed commands before continuing
230 for For loop (for ... in ... do ... done)
232 case Case statement ((case ... ) ... ;; ... esac)
234 esac Case statement end
236 while While loop (while ... do ... done)
238 do Do/For/While loop start (do ... until ...)
240 done For/While loop end
242 if Conditional statement (if ... else ... elif ... fi)
244 in For loop selection
246 then Conditional statement start
248 else Conditional statement alternative
250 elif Conditional statement end
254 fi Conditional statement end