-
Notifications
You must be signed in to change notification settings - Fork 30
/
sh.1
9131 lines (9123 loc) · 206 KB
/
sh.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.\"
.\" David Korn
.\" AT&T Bell Laboratories
.\"
.\" @(#)sh.1 (dgk@research.att.com) 12/28/93
.\"
.nr Z 1 \" set to 1 when command name is ksh, 2 for ksh93
.ds OK [\|
.ds CK \|]
.ds ' \s+4\v@.3m@\'\v@-.3m@\s-4
.ds ` \s+4\v@.3m@\`\v@-.3m@\s-4
.if \nZ=0 \{\
.TH SH 1
.\}
.if \nZ=1 \{\
.TH KSH 1
.\}
.if \nZ=2 \{\
.TH KSH93 1
.\}
.SH NAME
.if \nZ=0 \{\
sh, rsh \- shell, the standard/restricted command and programming language
.\}
.if \nZ=1 \{\
ksh, rksh \- KornShell, a standard/restricted command and programming language
.\}
.if \nZ=2 \{\
ksh93, rksh93 \- KornShell, a standard/restricted command and programming language
.\}
.SH SYNOPSIS
.if \nZ=0 \{\
.B sh
.\}
.if \nZ=1 \{\
.B ksh
.\}
.if \nZ=2 \{\
.B ksh93
.\}
[
.B \(+-abcefhiklmnprstuvxBCDEGH
] [
.B \(+-o
option ] .\|.\|. [
.B \-
] [ arg .\|.\|. ]
.br
.if \nZ=0 \{\
.B rsh
.\}
.if \nZ=1 \{\
.B rksh
.\}
.if \nZ=2 \{\
.B rksh93
.\}
[
.B \(+-abcefhiklmnpstuvxBCDEGH
] [
.B \(+-o
option ] .\|.\|. [
.B \-
] [ arg .\|.\|. ]
.SH DESCRIPTION
.if \nZ=0 .I Sh\^
.if \nZ=1 .I Ksh\^
.if \nZ=2 .I Ksh93\^
is a command and programming language
that executes commands read from a terminal
or a file.
.if \nZ=0 .I Rsh\^
.if \nZ=1 .I Rksh\^
.if \nZ=2 .I Rksh93\^
is a restricted version of the
.if \nZ=0 standard
command interpreter
.if \nZ=0 .IR sh ;
.if \nZ=1 .IR ksh ;
.if \nZ=2 .IR ksh93 ;
it is used to set up login names and execution environments whose
capabilities are more controlled than those of the standard shell.
See
.I Invocation\^
below
for the meaning of arguments to the shell.
.SS Definitions.
A
.I metacharacter\^
is one of the following characters:
.PP
.RS
\f3; & ( ) | < > new-line space tab\fP
.RE
.PP
A
.I blank\^
is a
.B tab
or a
.BR space .
An
.I identifier\^
is a sequence of letters, digits, or underscores
starting with a letter or underscore.
Identifiers are used as components of
.I variable\^
names.
A
.I vname\^
is a sequence of one or more identifiers
separated by a \fB\s+2.\s-2\fP and optionally preceded
by a \fB\s+2.\s-2\fP.
Vnames are used as function and variable names.
A
.I word\^
is a sequence of
.I characters\^
from the character set defined by the current locale,
excluding non-quoted
.IR metacharacters .
.PP
A
.I command\^
is a sequence of characters in the syntax
of the shell language.
The shell reads each command and
carries out the desired action either directly or by invoking
separate utilities.
A built-in command is a command that is carried out by the
shell itself without creating a separate process.
Some commands are built-in purely for convenience
and are not documented here.
Built-ins that cause
side effects in the shell environment and
built-ins that are found before performing a
path search (see
.I Execution\^
below)
are documented here.
For historical reasons, some of
these built-ins behave differently than
other built-ins and are called
.IR "special built-ins" .
.SS Commands.
A
.I simple-command\^
is a list of variable assignments
(see
.I Variable Assignments\^
below)
or a sequence of
.I blank\^
separated words
which may be preceded by a list of variable assignments
(see
.I Environment\^
below).
The first word specifies the name of the command to
be executed.
Except as specified below,
the remaining words are passed as arguments
to the invoked command.
The command name is passed as argument 0
(see
.IR exec (2)).
The
.I value\^
of a \fIsimple-command\fR is its exit status; 0-255
if it terminates normally; 256+\f2signum\^\fP if
it terminates abnormally (the name of the signal corresponding
to the exit status can be
obtained via the
.B \-l
option of the
.B kill\^
built-in utility).
.PP
A
.I pipeline\^
is a sequence of one or more
.I commands\^
separated by
.BR | .
The standard output of each command but the last
is connected by a
.IR pipe (2)
to the standard input of the next command.
Each command,
except possibly the last,
is run as a separate process;
the shell waits for the last command to terminate.
The exit status of a pipeline is the exit
status of the last command unless the
.B pipefail
option is enabled.
Each pipeline can be preceded by the
.I "reserved word"
.B !
which causes the exit status of the pipeline to become
0 if the exit status of the last command is non-zero, and
1 if the exit status of the last command is 0.
.PP
A
.I list\^
is a sequence of one or more
pipelines
separated by
.BR ; ,
.BR & ,
.BR |& ,
.BR && ,
or
.BR |\|| ,
and optionally terminated by
.BR ; ,
.BR & ,
or
.BR |& .
Of these five symbols,
.BR ; ,
.BR & ,
and
.B |&
have equal precedence,
which is lower than that of
.B &&
and
.BR |\|| .
The symbols
.B &&
and
.B |\||
also have equal precedence.
A semicolon
.RB ( ; )
causes sequential execution of the preceding pipeline; an ampersand
.RB ( & )
causes asynchronous execution of the preceding pipeline (i.e., the shell does
.I not\^
wait for that pipeline to finish).
The symbol
.B |&
causes asynchronous execution of the preceding pipeline
with a two-way pipe established to the parent shell;
the standard input and output of the spawned pipeline
can be written to and read from by the parent shell
by applying
the redirection operators
.B <&
and
.B >&
with arg
.B p
to commands and by using
.B \-p
option of
the built-in commands
.B read
and
.B print
described later.
The symbol
.B &&
.RB (\| |\|| \^)
causes the
.I list\^
following it to be executed only if the preceding
pipeline
returns a zero (non-zero) value.
One or more new-lines may appear in a
.I list\^
instead of a semicolon,
to delimit a command.
The first
.I item \^
of the first
.I pipeline\^
of a
.I list\^
that is a simple command not beginning
with a redirection, and not occurring within a
.BR while ,
.BR until ,
or
.B if
.IR list ,
can be preceded by a semicolon.
This semicolon
is ignored unless the
.B showme
option is enabled as described with
the
.B set
built-in below.
.PP
A
.I command\^
is either a \fIsimple-command\fR
or one of the following.
Unless otherwise stated,
the value returned by a command is that of the
last \fIsimple-command\fR executed in the command.
.TP
\f3for\fP \f2vname\^\fP \*(OK \f3in\fP \f2word\^\fP .\|.\|. \*(CK \f3;do\fP \f2list\^\fP \f3;done\fP
Each time a
.B for
command is executed,
.I vname\^
is set to the next
.I word\^
taken from the
.B in
.I word\^
list.
If
.BI in " word\^"
\&.\|.\|.
is omitted, then
the
.B for
command executes the \f3do\fP \f2list\^\fP once for each positional parameter
that is set starting from
.B 1
(see
.I "Parameter Expansion\^"
below).
Execution ends when there are no more words in the list.
.TP
\f3for ((\fP \*(OK\f2expr1\^\fP\*(CK \f3;\fP \*(OK\f2expr2\^\fP\*(CK \f3;\fP \*(OK\f2expr3\^\fP\*(CK \f3))\fP \f3;do\fP \f2list\^\fP \f3;done\fP
The arithmetic expression
.I expr1
is evaluated first
(see
.I "Arithmetic Evaluation"
below).
The arithmetic expression
.I expr2
is repeatedly evaluated until it evaluates to zero and when non-zero,
.I list
is executed and the arithmetic expression
.I expr3
evaluated.
If any expression
is omitted, then it behaves as if it evaluated to 1.
.TP
\f3select\fP \f2vname\^\fP \*(OK \f3in\fP \f2word\^\fP .\|.\|. \*(CK \f3;do\fP \f2list\^\fP \f3;done\fP
A
.B select
command prints on standard error (file descriptor 2) the set of
.IR word s,
each preceded by a number.
If
.BI in " word\^"
\&.\|.\|.
is omitted, then
the
positional parameters starting from
.B 1
are used instead
(see
.I "Parameter Expansion\^"
below).
The
.SM
.B PS3
prompt is printed
and a line is read from the standard input.
If this line consists of the number
of one of the listed
.IR word s,
then the value of the variable
.I vname\^
is set to the
.I word\^
corresponding to this number.
If this line is empty, the selection list is
printed again.
Otherwise the value of the variable
.I vname\^
is set to
.IR null .
The contents of the line read from standard input is
saved in
the variable
.BR REPLY .
The
.I list\^
is executed for each selection until a
.B break\^
or
.I end-of-file\^
is encountered.
If the
.SM
.B REPLY
variable is set to
.I null\^
by the execution of
.IR list ,
then the selection list is printed before
displaying the
.SM
.B PS3
prompt for the next selection.
.TP
\f3case\fP \f2word\^\fP \f3in\fP \*(OK \*(OK\f3(\fP\*(CK\f2pattern\^\fP \*(OK | \f2pattern\^\fP \*(CK .\|.\|. \f3)\fP \f2list\^\fP \f3;;\fP \*(CK .\|.\|. \f3esac\fP
A
.B case
command executes the
.I list\^
associated with the first
.I pattern\^
that matches
.IR word .
The form of the patterns is
the same as that used for
pathname expansion (see
.I "Pathname Expansion\^"
below).
The
.B ;;
operator causes execution of
.B case
to terminate.
If
.B ;&
is used in place of
.B ;;
the next subsequent list, if any, is executed.
.TP
\f3if\fP \f2list\^\fP \f3;then\fP \f2list\^\fP \*(OK \
\f3;elif\fP \f2list\^\fP \f3;then\fP \f2list\^\fP \*(CK .\|.\|. \
\*(OK \f3;else\fP \f2list\^\fP \*(CK \f3;f\&i\fP
The
.I list\^
following \f3if\fP is executed and,
if it
returns a zero exit status, the
.I list\^
following
the first
.B then
is executed.
Otherwise, the
.I list\^
following \f3elif\fP
is executed and, if its value is zero,
the
.I list\^
following
the next
.B then
is executed.
Failing each successive
.B elif
.IR list\^ ,
the
.B else
.I list\^
is executed.
If the
.B if
.I list\^
has non-zero exit status
and there is no
.B else
.IR list ,
then the
.B if
command returns a zero exit status.
.TP
.PD 0
\f3while\fP \f2list\^\fP \f3;do\fP \f2list\^\fP \f3;done\fP
.TP
\f3until\fP \f2list\^\fP \f3;do\fP \f2list\^\fP \f3;done\fP
.PD
A
.B while
command repeatedly executes the
.B while
.I list\^
and, if the exit status of the last command in the list is zero, executes
the
.B do
.IR list ;
otherwise the loop terminates.
If no commands in the
.B do
.I list\^
are executed, then the
.B while
command returns a zero exit status;
.B until
may be used in place of
.B while
to negate
the loop termination test.
.TP
.PD 0
\f3while\fP \f2inputredirection\^\fP \f3;do\fP \f2list\^\fP \f3;done\fP
.PD
Filescan loop. This is defined by a lone input redirection following
.B while
(see
.I Input/Output
below).
It is faster than using the
.B read
built-in command in a regular
.B while
loop.
The shell reads lines from the file or stream opened by
.I inputredirection\^
until the end is reached or the loop is broken.
For each line read,
the command
.I list\^
is executed with the line's contents assigned to the
.B REPLY
variable and the line's fields split into the positional parameters
(see
.I Field Splitting\^
and
.I Positional Parameters\^
below).
Within the
.IR list\^ ,
standard input is redirected to
.BR /dev/null .
If the
.B posix
compatibility shell option is on,
this loop type is disabled and
.I inputredirection\^
is processed like a lone redirection in any other context.
.TP
\f3((\fP\f2expression\^\fP\f3))\fP
.br
The
.I expression\^
is evaluated using the rules for arithmetic evaluation described below.
If the value of the arithmetic expression is non-zero, the exit
status is 0, otherwise the exit status is 1.
.TP
\f3(\fP\f2list\^\fP\f3)\fP
.br
Execute
.I list\^
in a separate environment.
Note, that if two adjacent open parentheses are
needed for nesting, a space must be inserted to avoid
evaluation as an arithmetic command as described above.
.TP
\f3{ \fP\f2list\^\fP\f3;}\fP
.br
.I list\^
is simply executed.
Note that unlike the metacharacters
.B (
and
.BR ) ,
.B {
and
.B }
are
.IR "reserved word" s
and must occur
at the beginning of a line or after a
.B ;
in order to be recognized.
.TP
\f3[[\fP\f2 expression \^\fP\f3]]\fP
.br
Evaluates
.I expression\^
and returns a zero exit status when
.I expression\^
is true.
See
.I "Conditional Expressions\^"
below, for a description of
.IR expression .
.TP
.PD 0
\f3function\fP \f2varname\^\fP \f3{\fP \f2list\^\fP \f3;}\fP
.TP
\f2varname\^\fP \f3() {\fP \f2list\^\fP \f3;}\fP
.PD
Define a function which is referenced by
.IR varname .
A function whose
.I varname\^
contains a
.B \s+2.\s-2
is called a discipline function and the portion
of the
.I varname\^
preceding the last
.B \s+2.\s-2
must refer to an existing variable.
The body of the function is the
.I list\^
of commands between
.B {
and
.BR } .
A function defined with the \f3function\fP \f2varname\^\fP
syntax can also be used as an argument to the \f3.\fP
special built-in command to get the equivalent behavior
as if the \f2varname\^\fP\f3()\fP syntax were used to define it.
(See
.I Functions\^
below.)
.TP
\f3namespace\fP \f2identifier\^\fP \f3{\fP \f2list\^\fP \f3;}\fP
.br
Defines or uses the name space
.I identifier\^
and runs the commands in
.I list\^
in this name space.
(See
.I Name Spaces\^
below.)
.TP
\f3&\fP \*(OK \f2name\^\fP \*(OK \f2arg...\^\fP \*(CK \*(CK
Causes subsequent
.I list\^
commands terminated by
.B &
to be placed in the background job pool
.IR name .
If
.I name\^
is omitted a default unnamed pool is used.
Commands in a named background pool may be executed remotely.
.TP
\f3time\fP \*(OK \f2pipeline\^\fP \*(CK
.br
If \f2pipeline\^\fP is omitted the user and system time for
the current shell and completed child processes is printed
on standard error.
Otherwise,
.I pipeline\^
is executed and the elapsed time as well as
the user and system time are printed on standard error.
The
.SM
.B TIMEFORMAT
variable may be set to a format string that specifies how the timing
information should be displayed.
See
.B "Shell Variables"
below
for a description of the
.SM
.B TIMEFORMAT
variable.
.PP
The following reserved words
are recognized as reserved only when they are the first word of a command
and are not quoted:
.PP
.if t .RS
.B
.if n if then else elif fi case esac for while until do done { } function select time [[ ]] !
.if t if then else elif fi case esac for while until do done { } function select time [[ ]] !
.if t .RE
.SS Variable Assignments.
One or more variable assignments can start a simple command
or can be arguments to the
.BR typeset ,
.BR enum ,
.BR export ,
or
.B readonly
special built-in commands as well as
to other declaration commands created as types.
The syntax for an \f2assignment\^\fP is of the form:
.TP
.PD 0
\f2varname\^\fP\f3=\fP\f2word\^\fP
.TP
\f2varname\^\fP\f3[\fP\f2word\^\fP\f3]\fP=\fP\f2word\^\fP
.PD
No space is permitted between \f2varname\^\fP and the \f3=\fP or
between \f3=\fP and \fIword\^\fP.
.TP
\f2varname\^\fP\f3=(\fP\f2assign_list\^\fP\f3)\fP
No space is permitted between \f2varname\^\fP and the \f3=\fP.
The variable
.I varname
is unset before the assignment.
An \f2assign_list\^\fP can be one of the following:
.RS 15
.PD 0
.TP
\f2word\^\fP ...
Indexed array assignment.
.TP
\f3[\fP\f2word\^\fP\f3]=\fP\f2word\^\fP .\|.\|.
Associative array assignment.
If preceded by
.B typeset \-a
this will create an indexed array instead.
.TP
\f2assignment\^\fP .\|.\|.
Compound variable assignment.
This creates a compound variable \f2varname\^\fP with
subvariables of the form \f2varname\^\fP\f3.\fP\f2name\^\fP,
where \f2name\^\fP is the name portion of \f2assignment\^\fP.
The value of \f2varname\^\fP will contain all the assignment elements.
Additional assignments made to subvariables of \f2varname\^\fP
will also be displayed as part of the value of \f2varname\^\fP.
If no \f2assignment\fPs are specified, \f2varname\^\fP will be
a compound variable allowing subsequence child elements to be defined.
.TP
\f3typeset\fP \*(OK\f2options\fP\*(CK \f2assignment\^\fP .\|.\|.
Nested variable assignment. Multiple assignments
can be specified by separating each of them with a \f3;\fP.
The previous value is unset before the assignment.
Other declaration commands such as
.BR readonly,
.BR enum ,
and
other declaration commands can be used in place of
.BR typeset .
.TP
\f3\|.\fP \f2filename\^\fP
Include the assignment commands contained in
.IR filename .
.PD
.RE
.PP
In addition, a \f3+=\fP can be used in place of the \f3=\fP
to signify adding to or appending to the previous value.
When \f3+=\fP is applied to an arithmetic type, \f2word\^\fP
is evaluated as an arithmetic expression and added to the current value.
When applied to a string variable, the value defined by \f2word\^\fP
is appended to the value. For compound assignments, the previous
value is not unset and the new values are appended to the
current ones provided that the types are compatible.
.PP
The right hand side of a variable assignment undergoes all the expansion
listed below except word splitting, brace expansion, and pathname expansion.
When the left hand side is an assignment is a compound variable and
the right hand is the name of a compound variable, the compound variable
on the right will be copied or appended to the compound variable on the left.
.SS Comments.
.PD 0
A word beginning with
.B #
causes that word and all the following characters up to a new-line
to be ignored.
.PD
.RE
.SS Aliasing.
The first word of each command is replaced by the text of an
.B alias
if an
.B alias
for this word has been defined.
An
.B alias
name consists of any number of characters excluding metacharacters,
quoting characters,
file expansion characters,
parameter expansion and command substitution
characters,
the characters
.B /
and
.BR = .
The replacement string can contain any
valid shell script
including the metacharacters listed above.
The first word of each command in the
replaced text,
other than
any that are in the process of being replaced,
will be tested for aliases.
If the last character of the alias value is a
.I blank\^
then the word following the alias will also be checked for alias
substitution.
Aliases can be used to redefine
built-in commands but cannot be used to redefine
the reserved words listed above.
Aliases can be created and listed with the
.B alias
command and can be removed with the
.B unalias
command.
.PP
.I Aliasing\^
is performed when
scripts are read,
not while they are executed.
Therefore,
for an alias to take effect,
the
.B alias
definition command has to be executed before
the command which references the alias is read.
.PP
The following aliases are automatically preset
when the shell is invoked as an interactive shell,
unless invoked in POSIX compliance mode
(see
.I Invocation\^
below).
Preset aliases can be unset or redefined.
.RS 20
.PD 0
.TP
.B "history=\(fmhist \-l\(fm"
.TP
.B "r=\(fmhist \-s\(fm"
.PD
.RE
.SS Tilde Expansion.
After alias substitution is performed, each word
is checked to see if it begins with an unquoted
.BR \(ap .
For tilde expansion,
.I word\^
also refers to the
.I word\^
portion of parameter expansion
(see
.I "Parameter Expansion\^"
below).
If a
.I word\^
is preceded by a tilde, then it is checked up to a
.B /
to see if it matches a user name in the
password database (see
.IR getpwname (3)).
If a match is found, the
.B \(ap
and the matched login name are replaced by the
login directory of the matched user.
If no match is found, the original text is left unchanged.
A
.B \(ap
by itself, or in front of a
.BR / ,
is replaced by
.BR $HOME ,
unless the
.B HOME
variable is unset, in which case
the current user's home directory as configured in the operating system
is used.
A
.B \(ap
followed by a
.B +
or
.B \-
is replaced by
.B
.SM $PWD
or
.B
.SM $OLDPWD
respectively.
.PP
In addition,
when expanding a
variable assignment (see
.I Variable Assignments
above),
tilde expansion is attempted when
the value of the assignment
begins with a
.BR \(ap ,
and when a
.B \(ap
appears after a
.BR : .
A
.B :
also terminates a user name following a
.BR \(ap .
.PP
The tilde expansion mechanism may be extended or modified
by defining one of the discipline functions
.B .sh.tilde.set
or
.B .sh.tilde.get
(see
.I Functions
and
.I Discipline Functions
below).
If either exists,
then upon encountering a tilde word to expand,
that function is called with the tilde word assigned to either
.B .sh.value
(for the
.B .sh.tilde.set
function) or
.B .sh.tilde
(for the
.B .sh.tilde.get
function).
Performing tilde expansion within a discipline function will not recursively
call that function, but default tilde expansion remains active,
so literal tildes should still be quoted where required.
Either function may assign a replacement string to
.BR .sh.value .
If this value is non-empty and does not start with a
.BR \(ap ,
it replaces the default tilde expansion when the function terminates.
Otherwise, the tilde expansion is left unchanged.
.SS Command Substitution.
The standard output from a command list enclosed in
parentheses preceded by a dollar sign (
\f3$(\fP\f2list\^\fP\f3)\fP
),
or in a brace group preceded by a dollar sign (
\f3${ \fP\f2list\^\fP\f3;}\fP
), or in a pair of grave accents (\^\f3\*`\^\*`\fP\^)
may be used as part or all
of a word;
trailing new-lines are removed.
In the second case, the \f3{\fP and \f3}\fP are treated as a reserved words
so that \f3{\fP must be followed by a \f2blank\^\fP and \f3}\fP must
appear at the beginning of the line or follow a \f3;\fP.
In the third (obsolete) form, the string between the quotes is processed
for special quoting characters before the command is executed (see
.I Quoting\^
below).
The command substitution
\^\f3$(\^cat file\^)\fP\^
can be replaced by the equivalent but faster
\^\f3$(\^<file\^)\fP\^.
The command substitution
\^\f3$(\^\fP\f2n\^\fP\f3<#\^)\fP
will expand to the current byte offset for file descriptor
.IR n .
Except for the second form, the command list is run in a subshell so that no
side effects are possible.
For the second form, the final
.B }
will be recognized as a reserved word after any token.
.SS Arithmetic Expansion.
An arithmetic expression enclosed in double
parentheses preceded by a dollar sign (
.B $((\|))
)
is replaced by the value of the arithmetic expression
within the double parentheses.
.SS Process Substitution.
Each command argument of the form
\f3<(\fP\f2list\^\fP\f3)\fP
or
\f3>(\fP\f2list\^\fP\f3)\fP
will run process
.I list
asynchronously connected to some file in
.B /dev/fd
if this directory exists, or else a fifo a temporary directory.
The name of this file will become the argument to the command.
If the form with
.B >
is selected then writing on this file will provide input for
.IR list .
If
.B <
is used,
then the file passed as an argument will contain the output of the
.I list
process.
For example,
.PP
.RS
\f3paste <(cut \-f1\fP \f2file1\fP\f3) <(cut \-f3\fP \f2file2\f3) | tee >(\fP\f2process1\fP\f3) >(\fP\f2process2\fP\f3)\fP
.RE
.PP
.I cuts
fields 1 and 3 from
the files
.I file1
and
.I file2
respectively,
.I pastes
the results together, and
sends it
to the processes
.I process1
and
.IR process2 ,
as well as putting it onto the standard output.
Note that the file, which is passed as an argument to the command,
is a UNIX