-
Notifications
You must be signed in to change notification settings - Fork 0
/
csh.1
2797 lines (2797 loc) · 68.5 KB
/
csh.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
.\" $OpenBSD: csh.1,v 1.83 2019/07/29 09:04:36 schwarze Exp $
.\" $NetBSD: csh.1,v 1.10 1995/03/21 09:02:35 cgd Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.1 8.2 (Berkeley) 1/21/94
.\"
.Dd $Mdocdate: July 29 2019 $
.Dt CSH 1
.Os
.Sh NAME
.Nm csh
.Nd a shell (command interpreter) with C-like syntax
.Sh SYNOPSIS
.Nm csh
.Op Fl bcefimnstVvXx
.Op Ar argument ...
.Nm csh
.Op Fl l
.Sh DESCRIPTION
.Nm
is a command language interpreter
incorporating a history mechanism (see
.Sx History substitutions ) ,
job control facilities (see
.Sx Jobs ) ,
interactive file name
and user name completion (see
.Sx File name completion ) ,
and a C-like syntax.
It is used both as an interactive
login shell and a shell script command processor.
.Ss Argument list processing
If the first argument (argument 0) to the shell is a dash
.Pq Sq \- ,
then this is a login shell.
A login shell also can be specified by invoking the shell with the
.Fl l
flag as the only argument.
.Pp
The rest of the flag arguments are interpreted as follows:
.Bl -tag -width 5n
.It Fl b
This flag forces a
.Dq break
from option processing, causing any further
shell arguments to be treated as non-option arguments.
The remaining arguments will not be interpreted as shell options.
This may be used to pass options to a shell script without confusion
or possible subterfuge.
The shell will not run a set-user-ID script without this option.
.It Fl c
Commands are read from the (single) following argument which must
be present.
Any remaining arguments are placed in
.Ar argv .
.It Fl e
The shell exits if any invoked command terminates abnormally
or yields a non-zero exit status.
.It Fl f
The shell will start faster, because it will neither search for nor
execute commands from the file
.Pa .cshrc
in the invoker's home directory.
Note: if the environment variable
.Ev HOME
is not set, fast startup is the default.
.It Fl i
The shell is interactive and prompts for its top-level input,
even if it appears not to be a terminal.
Shells are interactive without this option if their inputs
and outputs are terminals.
.It Fl l
The shell is a login shell (only applicable if
.Fl l
is the only flag specified).
.It Fl m
Read
.Pa .cshrc ,
regardless of its owner and group.
This option is dangerous and should only be used by
.Xr su 1 .
.It Fl n
Commands are parsed, but not executed.
This aids in syntactic checking of shell scripts.
When used interactively, the
shell can be terminated by pressing control-D (end-of-file character), since
.Ic exit
will not work.
.It Fl s
Command input is taken from the standard input.
.It Fl t
A single line of input is read and executed.
A backslash
.Pq Ql \e
may be used to escape the newline at the end of this
line and continue onto another line.
.It Fl V
Causes the
.Va verbose
variable to be set even before
.Pa .cshrc
is executed.
.It Fl v
Causes the
.Va verbose
variable to be set, with the effect
that command input is echoed after history substitution.
.It Fl X
Causes the
.Va echo
variable to be set even before
.Pa .cshrc
is executed.
.It Fl x
Causes the
.Va echo
variable to be set, so that commands are echoed immediately before execution.
.El
.Pp
After processing of flag arguments, if arguments remain but none of the
.Fl c ,
.Fl i ,
.Fl s ,
or
.Fl t
options were given, the first argument is taken as the name of a file of
commands to be executed.
The shell opens this file, and saves its name for possible resubstitution
by
.Sq $0 .
Since many systems use either the standard version 6 or version 7 shells
whose shell scripts are not compatible with this shell, the shell will
execute such a
.Dq standard
shell if the first character of a script
is not a hash mark
.Pq Ql # ;
i.e., if the script does not start with a comment.
Remaining arguments initialize the variable
.Va argv .
.Pp
An instance of
.Nm
begins by executing commands from the file
.Pa /etc/csh.cshrc
and,
if this is a login shell,
.Pa /etc/csh.login .
It then executes
commands from
.Pa .cshrc
in the home directory of the invoker,
and, if this is a login shell, the file
.Pa .login
in the same location.
It is typical for users on CRTs to put the command
.Ic stty crt
in their
.Pa .login
file, and to also invoke
.Xr tset 1
there.
.Pp
In the normal case, the shell will begin reading commands from the
terminal, prompting with
.Sq "% " .
Processing of arguments and the use of the shell to process files
containing command scripts will be described later.
.Pp
The shell repeatedly performs the following actions:
a line of command input is read and broken into
.Dq words .
This sequence of words is placed on the command history list and parsed.
Finally each command in the current line is executed.
.Pp
When a login shell terminates it executes commands from the files
.Pa .logout
in the user's home directory and
.Pa /etc/csh.logout .
.Ss Lexical structure
The shell splits input lines into words at blanks and tabs with the
following exceptions.
The characters
.Ql & ,
.Ql | ,
.Ql \&; ,
.Ql < ,
.Ql > ,
.Ql \&( ,
and
.Ql \&)
form separate words.
If doubled in
.Ql && ,
.Ql || ,
.Ql << ,
or
.Ql >> ,
these pairs form single words.
These parser metacharacters may be made part of other words, or have their
special meaning prevented, by preceding them with a backslash
.Pq Ql \e .
A newline preceded by a
.Ql \e
is equivalent to a blank.
.Pp
Strings enclosed in matched pairs of quotations,
.Ql \(aq ,
.Ql \` ,
or
.Ql \&" ,
form parts of a word; metacharacters in these strings, including blanks
and tabs, do not form separate words.
These quotations have semantics to be described later.
Within pairs of
.Ql \(aq
or
.Ql \&"
characters, a newline preceded by a
.Ql \e
gives
a true newline character.
.Pp
When the shell's input is not a terminal,
the character
.Ql #
introduces a comment that continues to the end of the
input line.
This special meaning is prevented when preceded by
.Ql \e
and in quotations using
.Ql \` ,
.Ql \(aq ,
and
.Ql \&" .
.Ss Commands
A simple command is a sequence of words, the first of which
specifies the command to be executed.
A simple command or
a sequence of simple commands separated by
.Ql |
characters forms a pipeline.
The output of each command in a pipeline is connected to the input of the next.
Sequences of pipelines may be separated by
.Ql \&; ,
and are then executed sequentially.
A sequence of pipelines may be executed without immediately
waiting for it to terminate by following it with a
.Ql & .
.Pp
Any of the above may be placed in
.Ql \&(
.Ql \&)
to form a simple command (that
may be a component of a pipeline, for example).
It is also possible to separate pipelines with
.Ql ||
or
.Ql &&
showing,
as in the C language,
that the second is to be executed only if the first fails or succeeds,
respectively.
(See
.Em Expressions . )
.Ss Jobs
The shell associates a
.Em job
with each pipeline.
It keeps a table of current jobs, printed by the
.Ic jobs
command, and assigns them small integer numbers.
When a job is started asynchronously with
.Ql & ,
the shell prints a line that looks
like:
.Bd -filled -offset indent
.Op 1
1234
.Ed
.Pp
showing that the job which was started asynchronously was job number
1 and had one (top-level) process, whose process ID was 1234.
.Pp
If you are running a job and wish to do something else you may hit
.Ic ^Z
(control-Z), which sends a
.Dv SIGSTOP
signal to the current job.
The shell will then normally show that the job has been
.Dq Stopped ,
and print another prompt.
You can then manipulate the state of this job, putting it in the
.Em background
with the
.Ic bg
command, or run some other
commands and eventually bring the job back into the
.Em foreground
with the
.Ic fg
command.
A
.Ic ^Z
takes effect immediately and
is like an interrupt in that pending output and unread input are discarded
when it is typed.
There is another special key
.Ic ^Y
that does not generate a
.Dv SIGSTOP
signal until a program attempts to
.Xr read 2
it.
This request can usefully be typed ahead when you have prepared some commands
for a job that you wish to stop after it has read them.
.Pp
A job being run in the background will stop if it tries to read
from the terminal.
Background jobs are normally allowed to produce output,
but this can be disabled by giving the command
.Ic stty tostop .
If you set this
tty option, then background jobs will stop when they try to produce
output like they do when they try to read input.
.Pp
There are several ways to refer to jobs in the shell.
The character
.Ql %
introduces a job name.
If you wish to refer to job number 1, you can name it as
.Ql %1 .
Just naming a job brings it to the foreground; thus
.Ic %1
is a synonym for
.Ic fg %1 ,
bringing job number 1 back into the foreground.
Similarly, saying
.Ic %1 &
resumes job number 1 in the background.
Jobs can also be named by prefixes of the string typed in to start them,
if these prefixes are unambiguous; thus
.Ic %ex
would normally restart a suspended
.Xr ex 1
job, if there were only one suspended job whose name began with
the string
.Qq ex .
It is also possible to say
.Ic %?string ,
which specifies a job whose text contains
.Ar string ,
if there is only one such job.
.Pp
The shell maintains a notion of the current and previous jobs.
In output about jobs, the current job is marked with a
.Ql +
and the previous job with a
.Ql \- .
The abbreviation
.Ql %+
refers to the current job and
.Ql %\-
refers to the previous job.
For close analogy with the syntax of the
.Ic history
mechanism (described below),
.Ql %%
is also a synonym for the current job.
.Pp
The job control mechanism requires that the
.Xr stty 1
option
.Ic new
be set.
It is an artifact from a
.Em new
implementation
of the
tty driver that allows generation of interrupt characters from
the keyboard to tell jobs to stop.
See
.Xr stty 1
for details
on setting options in the new tty driver.
.Ss Status reporting
The shell learns immediately whenever a process changes state.
It normally informs you whenever a job becomes blocked so that
no further progress is possible, but only just before it prints
a prompt.
This is done so that it does not otherwise disturb your work.
If, however, you set the shell variable
.Va notify ,
the shell will notify you immediately of changes of status in background
jobs.
There is also a shell command
.Ic notify
that marks a single process so that its status changes will be immediately
reported.
By default
.Ic notify
marks the current process;
simply say
.Ic notify
after starting a background job to mark it.
.Pp
When you try to leave the shell while jobs are stopped, you will
be warned that
.Dq You have stopped jobs .
You may use the
.Ic jobs
command to see what they are.
If you try to exit again immediately,
the shell will not warn you a second time, and the suspended
jobs will be terminated.
.Ss File name completion
When the file name completion feature is enabled by setting
the shell variable
.Va filec
(see
.Ic set ) ,
.Nm
will
interactively complete file names and user names from unique
prefixes when they are input from the terminal followed by
the escape character (the escape key, or control-[).
For example,
if the current directory looks like
.Bd -literal -offset indent
DSC.OLD bin cmd lib xmpl.c
DSC.NEW chaosnet cmtest mail xmpl.o
bench class dev mbox xmpl.out
.Ed
.Pp
and the input is
.Pp
.Dl % vi ch<escape>
.Pp
.Nm
will complete the prefix
.Dq ch
to the only matching file name
.Dq chaosnet ,
changing the input line to
.Pp
.Dl % vi chaosnet
.Pp
However, given
.Pp
.Dl % vi D<escape>
.Pp
.Nm
will only expand the input to
.Pp
.Dl % vi DSC.
.Pp
and will sound the terminal bell to indicate that the expansion is
incomplete, since there are two file names matching the prefix
.Ql D .
.Pp
If a partial file name is followed by the end-of-file character
(usually control-D), then, instead of completing the name,
.Nm
will list all file names matching the prefix.
For example, the input
.Pp
.Dl % vi D<control-D>
.Pp
causes all files beginning with
.Ql D
to be listed:
.Pp
.Dl DSC.NEW DSC.OLD
.Pp
while the input line remains unchanged.
.Pp
The same system of escape and end-of-file can also be used to
expand partial user names, if the word to be completed
(or listed) begins with the tilde character
.Pq Ql ~ .
For example, typing
.Pp
.Dl cd ~ro<escape>
.Pp
may produce the expansion
.Pp
.Dl cd ~root
.Pp
The use of the terminal bell to signal errors or multiple matches
can be inhibited by setting the variable
.Va nobeep .
.Pp
Normally, all files in the particular directory are candidates
for name completion.
Files with certain suffixes can be excluded
from consideration by setting the variable
.Va fignore
to the
list of suffixes to be ignored.
Thus, if
.Va fignore
is set by
the command
.Pp
.Dl % set fignore = (.o .out)
.Pp
then typing
.Pp
.Dl % vi x<escape>
.Pp
would result in the completion to
.Pp
.Dl % vi xmpl.c
.Pp
ignoring the files
.Qq xmpl.o
and
.Qq xmpl.out .
However, if the only completion possible requires not ignoring these
suffixes, then they are not ignored.
In addition,
.Va fignore
does not affect the listing of file names by control-D.
All files are listed regardless of their suffixes.
.Ss Substitutions
We now describe the various transformations the shell performs on the
input in the order in which they occur.
.Ss History substitutions
History substitutions place words from previous command input as portions
of new commands, making it easy to repeat commands, repeat arguments
of a previous command in the current command, or fix spelling mistakes
in the previous command with little typing and a high degree of confidence.
History substitutions begin with the character
.Ql \&!
and may begin
.Em anywhere
in the input stream (with the proviso that they do
.Em not
nest).
This
.Ql \&!
may be preceded by a
.Ql \e
to prevent its special meaning; for
convenience, a
.Ql \&!
character is passed unchanged when it is followed by a blank,
tab, newline,
.Ql =
or
.Ql \&( .
(History substitutions also occur when an input line begins with
.Ql ^ .
This special abbreviation will be described later.)
Any input line that contains history substitution is echoed on the terminal
before it is executed as it would have been typed without history substitution.
.Pp
Commands input from the terminal that consist of one or more words
are saved on the history list.
The history substitutions reintroduce sequences of words from these
saved commands into the input stream.
The size of the history list is controlled by the
.Va history
variable; the previous command is always retained,
regardless of the value of the history variable.
Commands are numbered sequentially from 1.
.Pp
For definiteness, consider the following output from the
.Ic history
command:
.Bd -literal -offset indent
09 write michael
10 ex write.c
11 cat oldwrite.c
12 diff *write.c
.Ed
.Pp
The commands are shown with their event numbers.
It is not usually necessary to use event numbers, but the current event
number can be made part of the prompt by placing a
.Ql \&!
in the prompt string.
.Pp
With the current event 13 we can refer to previous events by event
number
.Ql !11 ,
relatively as in
.Ql !\-2
(referring to the same event),
by a prefix of a command word
as in
.Ql !d
for event 12 or
.Ql !wri
for event 9, or by a string contained in
a word in the command as in
.Ql !?mic?
also referring to event 9.
These forms, without further change, simply reintroduce the words
of the specified events, each separated by a single blank.
As a special case,
.Ql !!
refers to the previous command; thus
.Ql !!
alone is a
.Em redo .
.Pp
To select words from an event we can follow the event specification by
a
.Ql \&:
and a designator for the desired words.
The words of an input line are numbered from 0,
the first (usually command) word being 0, the second word (first argument)
being 1, etc.
The basic word designators are:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It \&0
first (command) word
.It Ar n
.Ar n Ns 'th
argument
.It ^
first argument; i.e.,
.Ql 1
.It $
last argument
.It %
word matched by (immediately preceding)
.No \&? Ns Ar s Ns ?\&
search
.It Ar \&x\-y
range of words
.It Ar \&\-y
abbreviates
.Ql \&0\-y
.It *
abbreviates
.Ql ^\-$ ,
or nothing if only 1 word in event
.It Ar x*
abbreviates
.Ql x\-$
.It Ar x\-
like
.Ql x*
but omitting word
.Ql $
.El
.Pp
The
.Ql \&:
separating the event specification from the word designator
can be omitted if the argument selector begins with a
.Ql ^ ,
.Ql $ ,
.Ql * ,
.Ql \- ,
or
.Ql % .
After the optional word designator,
a sequence of modifiers can be placed, each preceded by a
.Ql \&: .
The following modifiers are defined:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It h
Remove a trailing pathname component, leaving the head.
.It r
Remove a trailing
.Ql .xxx
component, leaving the root name.
.It e
Remove all but the extension
.Ql .xxx
part.
.It s Ns Ar /l/r/
Substitute
.Ar l
for
.Ar r .
.It t
Remove all leading pathname components, leaving the tail.
.It \&&
Repeat the previous substitution.
.It g
Apply the change once on each word, prefixing the above; e.g.,
.Ql g& .
.It a
Apply the change as many times as possible on a single word, prefixing
the above.
It can be used together with
.Ql g
to apply a substitution globally.
.It p
Print the new command line but do not execute it.
.It q
Quote the substituted words, preventing further substitutions.
.It x
Like
.Ql q ,
but break into words at blanks, tabs, and newlines.
.El
.Pp
Unless preceded by a
.Ql g
the change is applied only to the first
modifiable word.
With substitutions, it is an error for no word to be applicable.
.Pp
The left-hand side of substitutions are not regular expressions in the sense
of the editors, but instead strings.
Any character may be used as the delimiter in place of
.Ql / ;
a
.Ql \e
quotes the delimiter into the
.Ar l
and
.Ar r
strings.
The character
.Ql &
in the right-hand side is replaced by the text from
the left.
A
.Ql \e
also quotes
.Ql & .
A
.Dv NULL
.Ar l
.Pq Ql //
uses the previous string either from an
.Ar l
or from a
contextual scan string
.Ar s
in
.Ql !? Ns Ar s Ns \e? .
The trailing delimiter in the substitution may be omitted if a newline
follows immediately as may the trailing
.Ql \&?
in a contextual scan.
.Pp
A history reference may be given without an event specification; e.g.,
.Ql !$ .
Here, the reference is to the previous command unless a previous
history reference occurred on the same line in which case this form repeats
the previous reference.
Thus
.Dq !?foo?^ !$
gives the first and last arguments
from the command matching
.Dq ?foo? .
.Pp
A special abbreviation of a history reference occurs when the first
non-blank character of an input line is a
.Ql ^ .
This is equivalent to
.Dq !:s^
providing a convenient shorthand for substitutions
on the text of the previous line.
Thus
.Ic ^lb^lib
fixes the spelling of
.Dq lib
in the previous command.
Finally, a history substitution may be surrounded with
.Ql {
and
.Ql }
if necessary to insulate it from the characters that follow.
Thus, after
.Ic ls \-ld ~paul
we might do
.Ic !{l}a
to do
.Ic ls \-ld ~paula ,
while
.Ic !la
would look for a command starting with
.Dq la .
.Ss Quotations with \(aq and \&"
The quotation of strings by
.Ql \(aq
and
.Ql \&"
can be used
to prevent all or some of the remaining substitutions.
Strings enclosed in
.Ql \(aq
are prevented from any further interpretation.
Strings enclosed in
.Ql \&"
may be expanded as described below.
.Pp
In both cases the resulting text becomes (all or part of) a single word;
only in one special case (see
.Em Command Substitution
below) does a
.Ql \&"
quoted string yield parts of more than one word;
.Ql \(aq
quoted strings never do.
.Ss Alias substitution
The shell maintains a list of aliases that can be established, displayed
and modified by the
.Ic alias
and
.Ic unalias
commands.
After a command line is scanned, it is parsed into distinct commands and
the first word of each command, left-to-right, is checked to see if it
has an alias.
If it does, then the text that is the alias for that command is reread
with the history mechanism available
as though that command were the previous input line.
The resulting words replace the
command and argument list.
If no reference is made to the history list, then the argument list is
left unchanged.
.Pp
Thus if the alias for
.Dq ls
is
.Dq ls \-l ,
the command
.Ic ls /usr
would map to
.Ic ls \-l /usr ,
the argument list here being undisturbed.
Similarly, if the alias for
.Dq lookup
was
.Dq grep !^ /etc/passwd
then
.Ic lookup bill
would map to
.Ic grep bill /etc/passwd .
.Pp
If an alias is found, the word transformation of the input text
is performed and the aliasing process begins again on the reformed input line.
Looping is prevented if the first word of the new text is the same as the old
by flagging it to prevent further aliasing.
Other loops are detected and cause an error.
.Pp
Note that the mechanism allows aliases to introduce parser metasyntax.
Thus, we can
.Ic alias print 'pr \e!* \&| lpr'
to make a command that
.Ic pr Ns 's
its arguments to the line printer.
.Ss Variable substitution
The shell maintains a set of variables, each of which has as value a list
of zero or more words.
Some of these variables are set by the shell or referred to by it.
For instance, the
.Va argv
variable is an image of the shell's argument list, and words of this
variable's value are referred to in special ways.
.Pp
The values of variables may be displayed and changed by using the
.Ic set
and
.Ic unset
commands.
Of the variables referred to by the shell a number are toggles;
the shell does not care what their value is,
only whether they are set or not.
For instance, the
.Va verbose
variable is a toggle that causes command input to be echoed.
The setting of this variable results from the
.Fl v
command-line option.
.Pp
Other operations treat variables numerically.
The
.Ic @
command permits numeric calculations to be performed and the result
assigned to a variable.
Variable values are, however, always represented as (zero or more) strings.
For the purposes of numeric operations, the null string is considered to be
zero, and the second and additional words of multiword values are ignored.
.Pp
After the input line is aliased and parsed, and before each command
is executed, variable substitution
is performed, keyed by
.Ql $
characters.
This expansion can be prevented by preceding the
.Ql $
with a
.Ql \e
except
within double quotes
.Pq Ql \&" ,
where it
.Em always
occurs, and within single quotes
.Pq Ql \(aq ,
where it
.Em never
occurs.
Strings quoted by backticks
.Pq \` \`
are interpreted later (see
.Sx Command substitution
below), so
.Ql $
substitution does not occur there until later, if at all.
A
.Ql $
is passed unchanged if followed by a blank, tab, or end-of-line.
.Pp
Input/output redirections are recognized before variable expansion,
and are variable expanded separately.
Otherwise, the command name and entire argument list are expanded together.
It is thus possible for the first (command) word (to this point) to generate
more than one word, the first of which becomes the command name,
and the rest of which become arguments.
.Pp
Unless enclosed in
.Ql \&"
or given the
.Ql :q
modifier, the results of variable
substitution may eventually be command and filename substituted.
Within
.Ql \&" ,
a variable whose value consists of multiple words expands to
(a portion of) a single word, with the words of the variable's value
separated by blanks.
When the
.Ql :q
modifier is applied to a substitution
the variable will expand to multiple words with each word separated
by a blank and quoted to prevent later command or filename substitution.
.Pp
The following metasequences are provided for introducing variable values into
the shell input.
Except as noted, it is an error to reference a variable that is not set.
.Pp
.Bl -tag -width Ds -compact -offset indent
.It $name
.It ${name}
Are replaced by the words of the value of variable
.Ar name ,
each separated by a blank.
Braces insulate
.Ar name
from following characters that would otherwise be part of it.
Shell variables have names consisting of up to 20 letters and digits
starting with a letter.
The underscore character is considered a letter.
If
.Ar name
is not a shell variable, but is set in the environment, then
that value is returned (but
.Ql \&:
modifiers and the other forms
given below are not available here).
.It $name Ns Op selector
.It ${name Ns [ selector ] Ns }
May be used to select only some of the words from the value of
.Ar name .
The selector is subjected to
.Ql $
substitution and may consist of a single
number or two numbers separated by a
.Ql \- .
The first word of a variable's value is numbered
.Ql 1 .