home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / ash / part01 next >
Text File  |  1989-05-29  |  49KB  |  1,411 lines

  1. Subject:  v19i001:  A reimplementation of the System V shell, Part01/08
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: ka@june.cs.washington.edu (Kenneth Almquist)
  7. Posting-number: Volume 19, Issue 1
  8. Archive-name: ash/part01
  9.  
  10. Ash is a reimplementation of the System V shell.  Currently it has most
  11. features of that shell, plus some additions.  It runs on 4.2 and 4.3BSD,
  12. SystemV releases 1, 2, 3, SystemIII, and perhaps Version 7.  Documentation
  13. includes a list of differences, a manpage, and internals documentation.
  14. It has several built-ins and shell functions.
  15.  
  16. Ash is copyrighted but there are few restrictions on what you can do
  17. with it.  Read the file LICENSE, which gives the details and explains
  18. about the lack of a warrantee.
  19.  
  20. ------------------------
  21. # This is part 1 of ash.  To unpack, feed it into the shell (not csh).
  22. # The ash distribution consists of eight pieces.  Be sure you get them all.
  23. # After you unpack everything, read the file README.
  24.  
  25. echo extracting README
  26. cat > README <<\EOF
  27.                  What is ash?
  28.  
  29. Ash is a reimplementation of the System V shell.  Currently it has
  30. most features of that shell, plus some additions.  The file ash.1
  31. contains the manual entry for ash.  The file DIFFERENCES lists known
  32. differences between ash and the standard shell.  The file TOUR provides
  33. some documentation of the internals.  Manual pages for some builtin
  34. commands are in the bltin subdirectory.
  35.  
  36. Ash is copyrighted but there are few restrictions on what you can do
  37. with it.  Read the file LICENSE, which gives the details and explains
  38. about the lack of a warrantee.
  39.  
  40.  
  41.                   Compiling
  42.  
  43. 1)  Create shell.h.  Three versions are provided with distribution:
  44.     shell.h.bsd    for 4.2/4.3 BSD
  45.     shell.h.s5r3    for System V release 3
  46.     shell.h.s5r2    for System III and System V release 1 and 2
  47.     For a Version 7 system, try using shell.h.s5r2 with SYSV undefined.
  48.  
  49. 2)  Type "make".  You may want to add DEBUG=-O to the make commands to
  50.     turn on optimization.  Also, use gcc rather than cc if you have it
  51.     (but remember that ANSI compilers like gcc won't work with the BSD
  52.     header files as distributed by Berkeley; if you use the unmodified
  53.     header files with gcc, job control won't work.)  If you don't have
  54.     an ANSI C compiler, you will get a lot of messages about illegal
  55.     pointer combinations; ignore them.  You can also ignore messages
  56.     from the loader telling you to run ranlib.
  57.  
  58. 3)  Install the commands where ever you feel like.  Besides ash, there
  59.     are several commands in the bltin directory:  catf, expr, echo, line,
  60.     nlecho test, [, and umask.  These are stand alone versions of builtin
  61.     commands.  Finally, the directory funcs contains shell functions.
  62.     Install these in a suitable directory and include them in your
  63.     path (e. g. PATH=$PATH:$HOME/ash/funcs%func).
  64.  
  65. If you are adventurous, you can compile with MALLOC=mymalloc.o, which
  66. will get you a version of malloc that uses a first fit allocation scheme.
  67. It may make more efficient use of memory than the standard version of
  68. malloc on your system.  The code was written for a VAX and is somewhat
  69. machine dependent.
  70. EOF
  71. if test `wc -c < README` -ne 2114
  72. then    echo 'README is the wrong size'
  73. fi
  74. if test ! -d bltin
  75. then    mkdir bltin
  76. fi
  77. if test ! -d funcs
  78. then    mkdir funcs
  79. fi
  80. echo extracting LICENSE
  81. cat > LICENSE <<\EOF
  82.             ASH GENERAL PUBLIC LICENSE
  83.  
  84.   1. You may copy and distribute ash code or code derived from it in
  85. source or object form, provided that you conspicuously and appropriately
  86. publish on each copy a valid copyright notice "Copyright 1989 by Kenneth
  87. Almquist." (or with whatever year is appropriate); keep intact the
  88. notices on all files that refer to this License Agreement and to the
  89. absence of any warranty; and give any other recipients of the ash program
  90. a copy of this License Agreement along with the program.
  91.  
  92.   2. You may not copy, sublicense, distribute or transfer ash except as
  93. expressly provided under this License Agreement.  Any attempt otherwise
  94. to copy, sublicense, distribute or transfer ash is void and your rights
  95. to use ash under this License agreement shall be automatically terminated.
  96. However, parties who have received computer software programs from you
  97. with this License Agreement will not have their licenses terminated so
  98. long as such parties remain in full compliance.
  99.  
  100.  
  101.                NO WARRANTY
  102.  
  103.   Because ash is licensed free of charge, I provide absolutely no
  104. warranty, to the extent permitted by applicable state law.  Except
  105. when otherwise stated in writing, Kenneth Almquist and/or other
  106. parties provide ash "as is" without warranty of any kind, either
  107. expressed or implied, including, but not limited to, the implied
  108. warranties of merchantability and fitness for a particular purpose.
  109. The entire risk as to the quality and performance of the program is
  110. with you.  Should the ash program prove defective, you assume the cost
  111. of all necessary servicing, repair or correction.
  112.  
  113.  In no event unless required by applicable law will Kenneth Almquist
  114. and/or any other party who may modify and redistribute ash as permitted
  115. above, be liable to you for damages, including any lost profits, lost
  116. monies, or other special, incidental or consequential damages arising
  117. out of the use or inability to use (including but not limited to loss
  118. of data or data being rendered inaccurate or losses sustained by third
  119. parties or a failure of the program to operate with programs provided
  120. by other parties) the program, even if you have been advised of the
  121. possibility of such damages, or for any claim by any other party.
  122. EOF
  123. if test `wc -c < LICENSE` -ne 2230
  124. then    echo 'LICENSE is the wrong size'
  125. fi
  126. rm -f bltin/LICENSE
  127. ln LICENSE bltin/LICENSE
  128. rm -f funcs/LICENSE
  129. ln LICENSE funcs/LICENSE
  130. echo extracting DIFFERENCES
  131. cat > DIFFERENCES <<\EOF
  132. If you are used to the Berkeley shell, there are a bunch of Sys-
  133. tem V features which will be new.  I give a partial list here:
  134.  
  135. 1.  Shell functions are included.  The syntax is:
  136.  
  137.         name () command
  138.  
  139.     Typically the command will be a list of commands enclosed
  140.     between "{" and "}".  The directory "funcs" contains a few
  141.     sample functions.
  142.  
  143. 2.  The -f option suppresses file name generation.
  144.  
  145. 3.  The syntax "[!...]" specifies a negated character class which
  146.     will match any character not in "...".
  147.  
  148. 4.  The first time you execute a command, the shell remembers
  149.     where it found it.  If a command is not found, nothing is
  150.     recorded.  If the location of a command changes, you can say
  151.     "hash command_name" to cause the command to be searched for
  152.     again, or you can say "hash -r" to cause it to forget the lo-
  153.     cation of all commands.
  154.  
  155. Differences between ash and the System V shell:
  156.  
  157. 5.  Shell functions can have local variables.  These are declared
  158.     by writing
  159.             local var1 var2 ...
  160.     at the top of the function.  As a special case, the name "-"
  161.     makes the shell options local to the function so that the
  162.     original values of the options will be restored when the
  163.     function returns.  Function arguments and the internal vari-
  164.     ables used by getopts are implicitly made local to a func-
  165.     tion.  This means that you can call a shell function, and
  166.     when the shell function returns the values of the positional
  167.     parameters ($1, $2, etc.) will be unchanged.
  168.  
  169. 6.  A directory of shell functions can be included in the search
  170.     path.  This is done by following the directory name by
  171.     "%func".  For example
  172.  
  173.         PATH=:/bin:/usr/bin:/usr/local/ashfuncs%func
  174.  
  175.     will cause the directory /usr/local/ashfuncs to be searched
  176.     for shell functions.  Each file in that directory should de-
  177.     fine a shell function with the same name as the file.  Some-
  178.     times it's convenient to combine several related functions
  179.     into a single file.  In this case there must be one link to
  180.     the file for each function (see ln(1)).  Note that once a
  181.     shell function has been read in, it will stay defined even if
  182.     PATH is changed or you do a "hash -r" command.
  183.  
  184. 7.  Function definitions override builtin commands.  In addition,
  185.     if the string "%builtin" appears in PATH then the list of
  186.     builtin commands will be search at that point in the path.
  187.     If the string "%builtin" does not appear in PATH, then the
  188.     list of builtin commands will be searched before PATH.
  189.  
  190. 8.  It is legal to have both a function and a variable with the
  191.     same name.  If you do this, then unset builtin will unset
  192.     both the variable and the name.
  193.  
  194. 9.  Ash allows you to negate a pattern by prefixing it with two
  195.     exclamation points.  For example,
  196.              ls src/!!*.o
  197.     lists all files in the src directory whose names do not end
  198.     in ".o".  Two exclamation points are required rather than one
  199.     to decrease the chances of users not familiar with this
  200.     feature invoking it by accident.
  201.  
  202. 10. Ash can simulate a /u directory containing the home direc-
  203.     tories of all the users on the system.  It is of course
  204.     better to have this handled by the file system if your system
  205.     supports symbolic links.
  206.  
  207. 11. The readonly and export commands allow you to assign a value
  208.     to a variable at the same time you export it or make it read
  209.     only.  For example:
  210.  
  211.         export TERM=2621
  212.  
  213.     sets the value of TERM to "2621" and then exports it.
  214.  
  215. 12. The method that sh uses to handle variables passed to it in
  216.     the environment appears to me to be unnecessarily confusing.
  217.     When ash starts up, it reads the environment, and for each
  218.     entry of the form "name=value", sets the variable "name" to
  219.     "value" and exports "name".
  220.  
  221. 13. The System V shell does strange things with backslashes in-
  222.     side back quotes.  In ash, commands inside back quotes are
  223.     parsed just like other commands.  Ash accepts "$(cmd)" as a
  224.     synomynm for "`cmd`".
  225.  
  226. 14. The login and newgrp builtins have been replaced with shell
  227.     functions.  The times and ulimit commands are not implement-
  228.     ed.
  229.  
  230. 15. The echo builtin has a "-n" option for compatibility with the
  231.     BSD echo.
  232.  
  233. 16. Test and expr have been merged into a single command and have
  234.     been built in.  Both commands accept the same syntax, which
  235.     is a merger of the operators provided by the two commands in-
  236.     dividually, except that the relational operators of expr have
  237.     been omitted.  I believe that the new command is slightly
  238.     more picky that the old test command about using strings that
  239.     are the same as operators; you should write
  240.  
  241.         test "'$line'" = "'X'"
  242.  
  243.     rather than
  244.  
  245.         test "$line" = ""
  246.  
  247.     The single quotes are used to indicate that the argument is a
  248.     string rather than an operator.  In the example I include
  249.     single quotes around both arguments to the "=" operator rath-
  250.     er than just the first one in order to make the test work
  251.     correctly on versions of test that don't treat single quotes
  252.     specially.
  253.  
  254.     Don't underestimate the power of regular expressions.  For
  255.     example, expr can compute substrings:
  256.  
  257.         y="`expr "$x" : ".\{4\}\(.\{0,3\}\)"`"
  258.  
  259.     sets y to the three character string of x beginning after the
  260.     fourth character.  If x is too short, it returns the all the
  261.     characters available.  (The System V syntax \{0,3\} matches 0
  262.     to 3 occurances of the preceding character; \{4\} is a
  263.     synonym for \{4,4\}.)
  264.  
  265. 17. The following flags are not implemented:  a, h, k, t, u, v.
  266.     These don't seem very useful to me (with the possible excep-
  267.     tion of -v), so I'm not likely to add them unless people ask.
  268.  
  269. 18. An interface to the Berkeley job control facilities is pro-
  270.     vided on systems where the kernel supports Berkeley job con-
  271.     trol, although not all the features of csh are included.  The
  272.     -j flag turns on job control.  (It is set on by default for
  273.     interactive shells.)  There are fg and bg commands function
  274.     more or less like the csh equivalents.
  275.  
  276.     The jobs command lists all the processes that you have start-
  277.     ed.  It is available even on systems without job control.
  278.  
  279. 19. The character ^ is not a synonym for |.
  280.  
  281. 20. In an interactive shell, the variable "$_" is set to the
  282.     value of the argument to the last command executed (exclusive
  283.     of commands inside executed inside shell functions).  This is
  284.     intended to be used in sequences like:
  285.             mkdir long_directory_name
  286.             cd $_
  287.     There is also a new builtin named "lc" (for last command)
  288.     which re-executed the last command (with no arguments) or de-
  289.     fines a function that will execute the command (if a function
  290.     name is given as an argument).  This is intended to make it
  291.     easier to re-execute a command.
  292.  
  293. I conclude by listing a few features that I have omitted inten-
  294. tionally.
  295.  
  296. 1.  Aliases.  Use functions instead.
  297.  
  298. 2.  Features that duplicate existing functionality.
  299.  
  300. 3.  History.  It seems to me that the csh history mechanism is
  301.     mostly a response to the deficiencies of UNIX terminal I/O.
  302.     Those of you running 4.2 BSD should try out atty (which I am
  303.     posting to the net at the same time as ash) and see if you
  304.     still want history.
  305.  
  306. 4.  Restricted shell.  Restricted shells tend to be insecure.
  307. EOF
  308. if test `wc -c < DIFFERENCES` -ne 7363
  309. then    echo 'DIFFERENCES is the wrong size'
  310. fi
  311. echo extracting ash.1
  312. cat > ash.1 <<\EOF
  313. .TH ASH 1
  314. .\"    Copyright (C) 1989 by Kenneth Almquist.
  315. .de h \" subheading
  316. .sp
  317. .ti -0.3i
  318. .B "\\$1"
  319. .PP
  320. ..
  321. .de d \" begin display
  322. .sp
  323. .in +4
  324. .nf
  325. ..
  326. .de e \" end display
  327. .in -4
  328. .fi
  329. .sp
  330. ..
  331. .de c \" command, etc.
  332. .br
  333. .HP 5
  334. \fB\\$1\fR
  335. .br
  336. ..
  337. .de b \" begin builtin command
  338. .HP 5
  339. .B \\$1
  340. ..
  341. .SH NAME
  342. ash \- a shell
  343. .SH SYNOPSIS
  344. .B ash
  345. [
  346. .B -efIijnsxz
  347. ] [
  348. .B +efIijnsxz
  349. ] [
  350. .B -c
  351. .I command
  352. ] [
  353. .I arg
  354. ] ...
  355. .SH COPYRIGHT
  356. .if n Copyright (C) 1989 by Kenneth Almquist.
  357. .if t Copyright \(co 1989 by Kenneth Almquist.  
  358. .SH DESCRIPTION
  359. .I Ash
  360. is a version of
  361. .I sh
  362. with features similar to those of the System V shell.
  363. This manual page lists all the features of
  364. .I ash
  365. but concentrates on the ones not in other shells.
  366. .h "Invocation"
  367. If the
  368. .B -c
  369. options is given, then the shell executes the specified shell command.
  370. The
  371. .B -s
  372. flag cause the shell to read commands from the standard input (after
  373. executing any command specified with the
  374. .B -c
  375. option.
  376. If neither the
  377. .B -s
  378. or
  379. .B -c
  380. options are set, then the first
  381. .I arg
  382. is taken as the name of a file to read commands from.
  383. If this is impossible because there are no arguments following
  384. the options, then
  385. .I ash
  386. will set the
  387. .B -s
  388. flag and will read commands from the standard input.
  389. .PP
  390. The shell sets the initial value of the positional parameters from the
  391. .IR arg s
  392. remaining after any
  393. .I arg
  394. used as the name of a file of commands is deleted.
  395. .PP
  396. The flags (other than
  397. .BR -c )
  398. are set by preceding them with ``-'' and cleared by preceding them
  399. with ``+''; see the
  400. .I set
  401. builtin command for a list of flags.
  402. If no value is specified for the
  403. .B -i
  404. flag, the
  405. .B -s
  406. flag is set, and the standard input and output of the shell
  407. are connected to terminals, then the
  408. .B -i
  409. flag will be set.
  410. If no value is specified for the
  411. .B -j
  412. flag, then the
  413. .B -j
  414. flag will be set if the
  415. .B -i
  416. flag is set.
  417. .PP
  418. When the shell is invoked with the
  419. .B -c
  420. option, it is good practice to include the
  421. .I -i
  422. flag if the command was entered interactively by a user.
  423. For compatibility with the System V shell, the
  424. .I -i
  425. option should come after the
  426. .B -c
  427. option.
  428. .PP
  429. If the first character of argument zero to the shell is ``-'',
  430. the shell is assumed to be a login shell, and the files
  431. .B /etc/profile
  432. and
  433. .B .profile
  434. are read if they exist.
  435. If the environment variable SHINIT is set on entry to the shell,
  436. the commands in SHINIT are normally parsed and executed.  SHINIT is
  437. not examined if the shell is a login shell, or if it the shell is running a
  438. shell procedure.   (A shell is considered to be running a shell
  439. procedure if neither the
  440. .B -s
  441. nor the
  442. .B -c
  443. options are set.)
  444. .h "Control Structures"
  445. A
  446. .I list
  447. is a sequence of zero or more commands separated by newlines,
  448. semicolons, or ampersands, and optionally terminated by one of these
  449. three characters.  (This differs from the System V shell, which
  450. requires a list to contain at least one command in most cases.)  The
  451. commands in a list are executed in the order they are written.
  452. If command is followed by an ampersand, the shell starts the command
  453. and immediately proceed onto the next command; otherwise it waits
  454. for the command to terminate before proceeding to the next one.
  455. .PP
  456. ``&&'' and ``||'' are binary operators.
  457. ``&&'' executes the first command, and then executes the second command
  458. iff the exit status of the first command is zero.  ``||'' is similar,
  459. but executes the second command iff the exit status of the first command
  460. is nonzero.  ``&&'' and ``||'' both have the same priority.
  461. .PP
  462. The ``|'' operator is a binary operator which feeds the standard output
  463. of the first command into the standard input of the second command.
  464. The exit status of the ``|'' operator is the exit status of the second
  465. command.  ``|'' has a higher priority than ``||'' or ``&&''.
  466. .PP
  467. An
  468. .I if
  469. command looks like
  470. .d
  471. \fBif\fR list
  472. \fBthen\fR    list
  473. .ti -\w'[ 'u
  474. [ \fBelif\fR list
  475.   \fBthen\fR    list ] ...
  476. .ti -\w'[ 'u
  477. [ \fBelse\fR    list ]
  478. \fBfi\fR
  479. .e
  480. .PP
  481. A
  482. .I while
  483. command looks like
  484. .d
  485. \fBwhile\fR list
  486. \fBdo\fR    list
  487. \fBdone\fR
  488. .e
  489. The two lists are executed repeatedly while the exit status of the first
  490. list is zero.  The
  491. .I until
  492. command is similar, but has the word
  493. .B until
  494. in place of
  495. .B while
  496.  repeats until the exit status of the first list
  497. is zero.
  498. .PP
  499. The
  500. .I for
  501. command looks like
  502. .d
  503. \fBfor\fR variable \fBin\fR word...
  504. \fBdo\fR    list
  505. \fBdone\fR
  506. .e
  507. The words are expanded, and then the list is executed repeatedly with
  508. the variable set to each word in turn.
  509. .B do
  510. and
  511. .B done
  512. may be replaced with
  513. ``{'' and ``}''.
  514. .PP
  515. The
  516. .I break
  517. and
  518. .I continue
  519. commands look like
  520. .d
  521. \fBbreak\fR [ num ]
  522. \fBcontinue\fR [ num ]
  523. .e
  524. .I Break
  525. terminates the
  526. .I num
  527. innermost
  528. .I for
  529. or
  530. .I while
  531. loops.
  532. .I Continue
  533. continues with the next iteration of the
  534. .IRnum'th
  535. innermost loop.
  536. These are implemented as builtin commands.
  537. .PP
  538. The
  539. .I case
  540. command looks like
  541. .d
  542. \fBcase\fR word \fBin\fR
  543. pattern\fB)\fR list \fB;;\fR
  544. \&...
  545. \fBesac\fR
  546. .e
  547. The pattern can actually be one or more patterns (see
  548. .I Patterns
  549. below), separated by ``|'' characters.
  550. .PP
  551. Commands may be grouped by writing either
  552. .d
  553. \fB(\fRlist\fB)\fR
  554. .e
  555. or
  556. .d
  557. \fB{\fR list; \fB}\fR
  558. .e
  559. The first of these executes the commands in a subshell.
  560. .PP
  561. A function definition looks like
  562. .d
  563. name \fB( )\fR command
  564. .e
  565. A function definition is an executable statement; when executed it installs
  566. a function named
  567. .B name
  568. and returns an exit status of zero.
  569. The command is normally a list enclosed between ``{'' and ``}''.
  570. .PP
  571. Variables may be declared to be local to a function by using a
  572. .I local
  573. command.  This should appear as the first staement of a function,
  574. and looks like
  575. .d
  576. \fBlocal\fR [ variable | \fB-\fR ] ...
  577. .e
  578. .I Local
  579. is implemented as a builtin command.
  580. .PP
  581. When a variable is made local, it inherits the initial value and
  582. exported and readonly flags from the variable with the same name in the
  583. surrounding scope, if there is one.  Otherwise, the variable is
  584. initially unset.
  585. .I Ash
  586. uses dynamic scoping, so that if you make the variable
  587. .B x
  588. local to function
  589. .IR f ,
  590. which then calls function
  591. .IR g ,
  592. references to the variable
  593. .B x
  594. made inside
  595. .I g
  596. will refer to the variable
  597. .B x
  598. declared inside
  599. .IR f ,
  600. not to the global variable named
  601. .BR x .
  602. .PP
  603. The only special parameter than can be made local is ``\fB-\fR''.
  604. Making ``\fB-\fR'' local any shell options that are changed via the
  605. .I set
  606. command inside the function to be restored to their original values
  607. when the function returns.
  608. .PP
  609. The
  610. .I return
  611. command looks like
  612. .d
  613. \fBreturn\fR [ exitstatus ]
  614. .e
  615. It terminates the currently executing function.
  616. .I Return
  617. is implemented as a builtin command.
  618. .h "Simple Commands"
  619. A simple command is a sequence of words.  The execution of a simple
  620. command proceeds as follows.  First, the leading words of the form
  621. ``name=value'' are stripped off and assigned to the environment of
  622. the command.  Second, the words are expanded.  Third, the first
  623. remaining word is taken as the command name that command is located.
  624. Fourth, any redirections are performed.  Fifth, the command is
  625. executed.  We look at these operations in reverse order.
  626. .PP
  627. The execution of the command varies with the type of command.
  628. There are three types of commands:  shell functions, builtin commands,
  629. and normal programs.
  630. .PP
  631. When a shell function is executed, all of the shell positional parameters
  632. (except $0, which remains unchanged) are set to the parameters to the shell
  633. function.  The variables which are explicitly placed in the environment
  634. of the command (by placing assignments to them before the function name)
  635. are made local to the function and are set to values given.
  636. Then the command given in the function definition is executed.
  637. The positional parameters are restored to their original values when
  638. the command completes.
  639. .PP
  640. Shell builtins are executed internally to the shell, without spawning
  641. a new process.
  642. .PP
  643. When a normal program is executed, the shell runs the program, passing
  644. the parameters and the environment to the program.  If the program is
  645. a shell procedure, the shell will interpret the program in a subshell.
  646. The shell will reinitialize itself in this case, so that the effect
  647. will be as if a new shell had been invoked to handle the shell procedure,
  648. except that the location of commands located in the parent shell will
  649. be remembered by the child.  If the program is a file beginning with
  650. ``#!'', the remainder of the first line specifies an interpreter for
  651. the program.  The shell (or the operating system, under Berkeley UNIX)
  652. will run the interpreter in this case.  The arguments to the interpreter
  653. will consist of any arguments given on the first line of the program,
  654. followed by the name of the program, followed by the arguments passed
  655. to the program.
  656. .h "Redirection"
  657. Input/output redirections can be intermixed with the words in a simple
  658. command and can be placed following any of the other commands.  When
  659. redirection occurs, the shell saves the old values of the file descriptors
  660. and restores them when the command completes.  The ``<'', ``>'', and ``>>''
  661. redirections open a file for input, output, and appending, respectively.
  662. The ``<&digit'' and ``>&digit'' makes the input or output a duplicate
  663. of the file descriptor numbered by the digit.  If a minus sign is used
  664. in place of a digit, the standard input or standard output are closed.
  665. .PP
  666. The ``<<\ word'' redirection
  667. takes input from a
  668. .I here
  669. document.
  670. As the shell encounters ``<<'' redirections, it collects them.  The
  671. next time it encounters an unescaped newline, it reads the documents
  672. in turn.  The word following the ``<<'' specifies the contents of the
  673. line that terminates the document.  If none of the quoting methods
  674. ('', "", or \e) are used to enter the word, then the document is treated
  675. like a word inside double quotes:  ``$'' and backquote are expanded
  676. and backslash can be used to escape these and to continue long lines.
  677. The word cannot contain any variable or command substitutions, and
  678. its length (after quoting) must be in the range of 1 to 79 characters.
  679. If ``<<-'' is used in place of ``<<'', then leading tabs are deleted
  680. from the lines of the document.  (This is to allow you do indent shell
  681. procedures containing here documents in a natural fashion.)
  682. .PP
  683. Any of the preceding redirection operators may be preceded by a single
  684. digit specifying the file descriptor to be redirected.  There cannot
  685. be any white space between the digit and the redirection operator.
  686. .h "Path Search"
  687. When locating a command, the shell first looks to see if it has a
  688. shell function by that name.  Then, if PATH does not contain an
  689. entry for "%builtin", it looks for a builtin command by that name.
  690. Finally, it searches each entry in PATH in turn for the command.
  691. .PP
  692. The value of the PATH variable should be a series of entries separated
  693. by colons.
  694. Each entry consists of a directory name, or a directory name followed
  695. by a flag beginning with a percent sign.
  696. The current directory should be indicated by an empty directory name.
  697. .PP
  698. If no percent sign is present, then the entry causes the shell to
  699. search for the command in the specified directory.  If the flag is
  700. ``%builtin'' then the list of shell builtin commands is searched.
  701. If the flag is ``%func'' then the directory is searched for a file which
  702. is read as input to the shell.  This file should define a function
  703. whose name is the name of the command being searched for.
  704. .PP
  705. Command names containing a slash are simply executed without performing
  706. any of the above searches.
  707. .h "The Environment"
  708. The environment of a command is a set of name/value pairs.  When the
  709. shell is invoked, it reads these names and values, sets the shell
  710. variables with these names to the corresponding values, and marks
  711. the variables as exported.  The
  712. .I export
  713. command can be used to mark additional variables as exported.
  714. .PP
  715. The environment of a command is constructed by constructing name/value
  716. pairs from all the exported shell variables, and then modifying this
  717. set by the assignments which precede the command, if any.
  718. .h "Expansion"
  719. The process of evaluating words when a shell procedure is executed is
  720. called
  721. .IR expansion .
  722. Expansion consists of four steps:  variable substitution, command
  723. substitution, word splitting, and file name generation.  If a word
  724. is the expression following the word
  725. .B case
  726. in a case statement, the file name
  727. which follows a redirection symbol, or an assignment to the environment
  728. of a command, then the word cannot be split into multiple words.  In
  729. these cases, the last two steps of the expansion process are omitted.
  730. .h "Variable Substitution"
  731. To be written.
  732. .h "Command Substitution"
  733. .I Ash
  734. accepts two syntaxes for command substitution:
  735. .b
  736. `\fIlist\fR`
  737. .e
  738. and
  739. .b
  740. $(\fIlist\fR)
  741. .e
  742. Either of these may be included in a word.
  743. During the command substitution process, the command (syntactly a
  744. .IR list )
  745. will be executed and anything that the command writes to the standard
  746. output will be captured by the shell.  The final newline (if any) of
  747. the output will be deleted; the rest of the output will be substituted
  748. for the command in the word.
  749. .h "Word Splitting"
  750. When the value of a variable or the output of a command is substituted,
  751. the resulting text is subject to word splitting, unless the dollar sign
  752. introducing the variable or backquotes containing the text were enclosed
  753. in double quotes.  In addition, ``$@'' is subject to a special type of
  754. splitting, even in the presence of double quotes.
  755. .PP
  756. Ash uses two different splitting algorithms.  The normal approach, which
  757. is intended for splitting text separated by which space, is used if the
  758. first character of the shell variable IFS is a space.  Otherwise an alternative
  759. experimental algorithm, which is useful for splitting (possibly empty)
  760. fields separated by a separator character, is used.
  761. .PP
  762. When performing splitting, the shell scans the replacement text looking
  763. for a character (when IFS does not begin with a space) or a sequence of
  764. characters (when IFS does begin with a space), deletes the character or
  765. sequence of characters, and spits the word into two strings at that
  766. point.  When IFS begins with a space, the shell deletes either of the
  767. strings if they are null.  As a special case, if the word containing
  768. the replacement text is the null string, the word is deleted.
  769. .PP
  770. The variable ``$@'' is special in two ways.  First, splitting takes
  771. place between the positional parameters, even if the text is enclosed
  772. in double quotes.  Second, if the word containing the replacement
  773. text is the null string and there are no positional parameters, then
  774. the word is deleted.  The result of these rules is that "$@" is
  775. equivalent to "$1" "$2" ... "$\fIn\fR", where \fIn\fR is the number of
  776. positional parameters.  (Note that this differs from the System V shell.
  777. The System V documentation claims that "$@" behaves this way; in fact
  778. on the System V shell "$@" is equivalent to "" when there are no
  779. positional paramteters.)
  780. .h "File Name Generation"
  781. Unless the
  782. .B -f
  783. flag is set, file name generation is performed after word splitting is
  784. complete.  Each word is viewed as a series of patterns, separated by
  785. slashes.  The process of expansion replaces the word with the names of
  786. all existing files whose names can be formed by replacing each pattern
  787. with a string that matches the specified pattern.  There are two
  788. restrictions on this:  first, a pattern cannot match a string containing
  789. a slash, and second, a pattern cannot match a string starting with a
  790. period unless the first character of the pattern is a period.
  791. .PP
  792. If a word fails to match any files and the
  793. .B -z
  794. flag is not set, then the word will be left unchanged (except that the
  795. meta-characters will be converted to normal characters).  If the
  796. .B -z
  797. flag is set, then the word is only left unchanged if none
  798. of the patterns contain a character that can match anything besides
  799. itself.  Otherwise the
  800. .B -z
  801. flag forces the word to be replaced with the names of the files that it
  802. matches, even if there are zero names.
  803. .h "Patterns"
  804. A
  805. .I pattern
  806. consists of normal characters, which match themselves, and meta-characters.
  807. The meta-characters are ``!'', ``*'', ``?'', and ``[''.  These characters lose
  808. there special meanings if they are quoted.  When command or variable
  809. substitution is performed and the dollar sign or back quotes are not
  810. double quoted, the value of the variable or the output of the command
  811. is scanned for these characters and they are turned into meta-characters.
  812. .PP
  813. Two exclamation points at the beginning of a pattern function as a ``not''
  814. operator, causing the pattern to match any string that the remainder of
  815. the pattern does
  816. .I not
  817. match.  Other occurances of exclamation points in a pattern match
  818. exclamation points.  Two exclamation points are required rather than one
  819. to decrease the incompatibility with the System V shell (which does not
  820. treat exclamation points specially).
  821. .PP
  822. An asterisk (``*'') matches any string of characters.
  823. A question mark matches any single character.
  824. A left bracket (``['') introduces a character class.  The end of the
  825. character class is indicated by a ``]''; if the ``]'' is missing then
  826. the ``['' matches a ``['' rather than introducing a character class.
  827. A character class matches any of the characters between the square
  828. brackets.  A range of characters may be specified using a minus sign.
  829. The character class may be complemented by making an exclamation point
  830. the first character of the character class.
  831. .PP
  832. To include a ``]'' in a character class, make it the first character listed
  833. (after the ``!'', if any).
  834. To include a minus sign, make it the first or last character listed.
  835. .h "The /u Directory"
  836. By convention, the name ``/u/user'' refers to the home directory of the
  837. specified user.  There are good reasons why this feature should be supported
  838. by the file system (using a feature such as symbolic links) rather than
  839. by the shell, but
  840. .I ash
  841. is capable of performing this mapping if the file system doesn't.
  842. If the mapping is done by
  843. .IR ash ,
  844. setting the
  845. .B -f
  846. flag will turn it off.
  847. .h "Character Set"
  848. .I Ash
  849. silently discards nul characters.  Any other character will be handled
  850. correctly by
  851. .IR ash ,
  852. including characters with the high order bit set.
  853. .h "Job Names and Job Control"
  854. The term
  855. .I job
  856. refers to a process created by a shell command, or in the case of a
  857. pipeline, to the set of processes in the pipeline.  The ways to refer
  858. to a job are:
  859. .b
  860. %\fInumber\fR
  861. %\fIstring\fR
  862. %%
  863. \fIprocess_id\fR
  864. .e
  865. The first form identifies a job by job number.
  866. When a command is run,
  867. .I ash
  868. assigns it a job number
  869. (the lowest unused number is assigned).
  870. The second form identifies a job by giving a prefix of the command used
  871. to create the job.  The prefix must be unique.  If there is only one job,
  872. then the null prefix will identify the job, so you can refer to the job
  873. by writing ``%''.  The third form refers to the \fIcurrent job\fR.  The
  874. current job is the last job to be stopped while it was in the foreground.
  875. (See the next paragraph.)  The last form identifies a job by giving the
  876. process id of the last process in the job.
  877. .PP
  878. If the operating system that
  879. .I ash
  880. is running on supports job control,
  881. .I ash
  882. will allow you to use it.
  883. In this case, typing the suspend character (typically ^Z) while running
  884. a command will return you to
  885. .I ash
  886. and will make the suspended command the current job.  You can then continue
  887. the job in the background by typing
  888. .IR bg ,
  889. or you can continue it in the foreground by typing
  890. .IR fg .
  891. .h "Atty"
  892. If the shell variable ATTY is set, and the shell variable TERM is not
  893. set to ``emacs'', then \fIash\fR generates appropriate escape sequences
  894. to talk to
  895. .IR atty (1).
  896. .h "Exit Statuses"
  897. By tradition, an exit status of zero means that a command has succeeded
  898. and a nonzero exit status indicates that the command failed.  This is
  899. better than no convention at all, but in practice it is extremely useful
  900. to allow commands that succeed to use the exit status to return information
  901. to the caller.  A variety of better conventions have been proposed, but
  902. none of them has met with universal approval.  The convention used by
  903. \fIash\fR and all the programs included in the \fIash\fR distribution is
  904. as follows:
  905. .ta 1i,2i
  906. .nf
  907.     0    Success.
  908.     1    Alternate success.
  909.     2    Failure.
  910.     129-...    Command terminated by a signal.
  911. .fi
  912. The \fIalternate success\fR return is used by commands to indicate various
  913. conditions which are not errors but which can, with a little imagination,
  914. be conceived of as less successful than plain success.  For example,
  915. .I test
  916. returns 1 when the tested condition is false and
  917. .I getopts
  918. returns 1 when there are no more options.
  919. Because this convention is not used universally, the
  920. .B -e
  921. option of
  922. .I ash
  923. causes the shell to exit when a command returns 1 even though that
  924. contradicts the convention described here.
  925. .PP
  926. When a command is terminated by a signal, the uses 128 plus the signal
  927. number as the exit code for the command.
  928. .h "Builtin Commands"
  929. This concluding section lists the builtin commands which are builtin
  930. because they need to perform some operation that can't be performed by a
  931. separate process.  In addition to these, there are several other commands
  932. .RI ( catf ,
  933. .IR echo ,
  934. .IR expr ,
  935. .IR line ,
  936. .IR nlecho ,
  937. .IR test ,
  938. .RI  `` : '',
  939. and
  940. .IR true )
  941. which can optionally be compiled into the shell.  The builtin
  942. commands described below that accept options use the System V Release 2
  943. .IR getopt (3)
  944. syntax.
  945. .sp
  946. .b bg
  947. [
  948. .I job
  949. ] ...
  950. .br
  951. Continue the specified jobs (or the current job if no jobs are given)
  952. in the background.
  953. This command is only available on systems with Bekeley job control.
  954. .b bltin
  955. .IR "command arg" ...
  956. .br
  957. Execute the specified builtin command.  (This is useful when you have a
  958. shell function with the same name as a builtin command.)
  959. .b cd
  960. [
  961. .I directory
  962. ]
  963. .br
  964. Switch to the specified directory (default $HOME).
  965. If the an entry for CDPATH appears in the environment of the cd command
  966. or the shell variable CDPATH is set and the directory name does not
  967. begin with a slash, then the directories listed in CDPATH will be
  968. searched for the specified directory.  The format of CDPATH is the
  969. same as that of PATH.
  970. In an interactive shell, the cd command will print out the name of the
  971. directory that it actually switched to if this is different from the
  972. name that the user gave.  These may be different either because
  973. the CDPATH mechanism was used or because a symbolic link was crossed.
  974. .b ".\fI\h'0.1i'file"
  975. .br
  976. The commands in the specified file are read and executed by the shell.
  977. A path search is not done to find the file because the directories in
  978. PATH generally contain files that are intended to be executed, not read.
  979. .b eval
  980. .IR string ...
  981. .br
  982. The strings are parsed as shell commands and executed.
  983. (This differs from the System V shell, which concatenates the arguments
  984. (separated by spaces) and parses the result as a single command.)
  985. .b exec
  986. [
  987. .IR "command arg" ...
  988. ]
  989. .br
  990. Unless
  991. .I command
  992. is omitted,
  993. the shell process is replaced with the specified program (which must be a real
  994. program, not a shell builtin or function).
  995. Any redirections on the exec command are marked as permanent, so that they
  996. are not undone when the exec command finishes.
  997. If the command is not found, the exec command causes the shell to exit.
  998. .b exit
  999. [
  1000. .I exitstatus
  1001. ]
  1002. .br
  1003. Terminate the shell process.  If
  1004. .I exitstatus
  1005. is given it is used as the
  1006. exit status of the shell; otherwise the exit status of the preceding
  1007. command is used.
  1008. .b export
  1009. .IR name ...
  1010. .br
  1011. The specified names are exported so that they will appear in the environment
  1012. of subsequent commands.  The only way to un-export a variable is to unset it.
  1013. .I Ash
  1014. allows the value of a variable to be set at the same time it is exported
  1015. by writing
  1016. .d
  1017. \fBexport\fR name=value
  1018. .e
  1019. With no arguments the export command lists the names of all exported variables.
  1020. .b fg
  1021. [
  1022. .I job
  1023. ]
  1024. .br
  1025. Move the specified job or the current job to the foreground.
  1026. This command is only available on systems with Bekeley job control.
  1027. .b getopts
  1028. .I optstring
  1029. .I var
  1030. .br
  1031. The System V
  1032. .I getopts
  1033. command.
  1034. .b hash
  1035. .B -rv
  1036. .IR command ...
  1037. .br
  1038. The shell maintains a hash table which remembers the locations of
  1039. commands.  With no arguments whatsoever, the hash command prints
  1040. out the contents of this table.  Entries which have not been looked
  1041. at since the last
  1042. .I cd
  1043. command are marked with an asterisk; it is possible for these entries
  1044. to be invalid.
  1045. .sp
  1046. With arguments, the hash command removes the specified commands from
  1047. the hash table (unless they are functions) and then locates them.
  1048. With the
  1049. .B -v
  1050. option,
  1051. .I hash
  1052. prints the locations of the commands as it finds them.
  1053. The
  1054. .B -r
  1055. option causes the
  1056. .I hash
  1057. command to delete all the entries in the hash table except for
  1058. functions.
  1059. .b jobid
  1060. [
  1061. .I job
  1062. ]
  1063. .br
  1064. Print the process id's of the processes in the job.  If the job argument
  1065. is omitted, use the current job.
  1066. .b jobs
  1067. .br
  1068. This command lists out all the background processes which are children
  1069. of the current shell process.
  1070. .b lc
  1071. [
  1072. .I function-name
  1073. ]
  1074. .br
  1075. The function name is defined to execute the last command entered.
  1076. If the function name is omitted, the last command executed is
  1077. executed again.  This command only works if the
  1078. .B -i
  1079. flag is set.
  1080. .b pwd
  1081. .br
  1082. Print the current directory.  The builtin command may differ from the
  1083. program of the same name because the builtin command remembers what
  1084. the current directory is rather than recomputing it each time.  This
  1085. makes it faster.  However, if the current directory is renamed, the
  1086. builtin version of pwd will continue to print the old name for the
  1087. directory.
  1088. .b read
  1089. [
  1090. .B -p
  1091. .I prompt
  1092. ]
  1093. [
  1094. .B -e
  1095. ]
  1096. .IR variable ...
  1097. .br
  1098. The prompt is printed if the
  1099. .B -p
  1100. option is specified and the standard input is a terminal.  Then a
  1101. line is read from the standard input.  The trailing newline is deleted
  1102. from the line and the line is split as described
  1103. in the section on word splitting above, and the pieces are assigned to
  1104. the variables in order.  If there are more pieces than variables, the
  1105. remaining pieces (along with the characters in IFS that separated them)
  1106. are assigned to the last variable.  If there are more variables than
  1107. pieces, the remaining variables are assigned the null string.
  1108. .sp
  1109. The
  1110. .B -e
  1111. option causes any backslashes in the input to be treated specially.
  1112. If a backslash is followed by a newline, the backslash and the newline
  1113. will be deleted.  If a backslash is followed by any other character,
  1114. the backslash will be deleted and the following character will be treated
  1115. as though it were not in IFS, even if it is.
  1116. .b readonly
  1117. .IR name ...
  1118. .br
  1119. The specified names are marked as read only, so that they cannot be
  1120. subsequently modified or unset.
  1121. .I Ash
  1122. allows the value of a variable to be set at the same time it is marked
  1123. read only by writing
  1124. .d
  1125. \fBreadonly\fR name=value
  1126. .e
  1127. With no arguments the readonly command lists the names of all
  1128. read only variables.
  1129. .b set
  1130. [
  1131. {
  1132. .BI - options
  1133. |
  1134. .BI + options
  1135. |
  1136. .B --
  1137. }
  1138. ]
  1139. .IR arg ...
  1140. .br
  1141. The
  1142. .I set
  1143. command performs three different functions.
  1144. .sp
  1145. With no arguments, it lists the values of all shell variables.
  1146. .sp
  1147. If options are given, it sets the specified option flags, or clears
  1148. them if the option flags are introduced with a
  1149. .B +
  1150. rather than a
  1151. .BR - .
  1152. Only the first argument to
  1153. .I set
  1154. can contain options.
  1155. The possible options are:
  1156. .sp
  1157. .ta 0.4i
  1158. .in +0.4i
  1159. .ti -0.4i
  1160. \fB-e\fR    Causes the shell to exit when a command terminates with
  1161. a nonzero exit status, except when the exit status of the command is
  1162. explicitly tested.  The exit status of a command is considered to be
  1163. explicitly tested if the command is used to control an
  1164. .IR if ,
  1165. .IR elif ,
  1166. .IR while ,
  1167. or
  1168. .IR until ;
  1169. or if the command is the left hand operand of an ``&&'' or ``||''
  1170. operator.
  1171. .sp
  1172. .ti -0.4i
  1173. \fB-f\fR    Turn off file name generation.
  1174. .sp
  1175. .ti -0.4i
  1176. \fB-I\fR    Cause the shell to ignore end of file conditions.
  1177. (This doesn't apply when the shell a script sourced using the ``.''
  1178. command.)  The shell will in fact exit if it gets 50 eof's in a
  1179. row.
  1180. .sp
  1181. .ti -0.4i
  1182. \fB-i\fR    Make the shell interactive.  This causes the shell to
  1183. prompt for input, to trap interrupts, to ignore quit and terminate signals,
  1184. and to return to the main command loop rather than exiting on error.
  1185. .sp
  1186. .ti -0.4i
  1187. \fB-j\fR    Turns on Berkeley job control, on systems that support it.
  1188. When the shell starts up, the
  1189. .B -j
  1190. is set by default if the
  1191. .B -i
  1192. flag is set.
  1193. .sp
  1194. .ti -0.4i
  1195. \fB-n\fR    Causes the shell to read commands but not execute them.
  1196. (This is marginally useful for checking the syntax of scripts.)
  1197. .sp
  1198. .ti -0.4i
  1199. \fB-s\fR    If this flag is set when the shell starts up, the shell
  1200. reads commands from its standard input.  The shell doesn't examine the
  1201. value of this flag any other time.
  1202. .sp
  1203. .ti -0.4i
  1204. \fB-x\fR    If this flag is set, the shell will print out each
  1205. command before executing it.
  1206. .sp
  1207. .ti -0.4i
  1208. \fB-z\fR    If this flag is set, the file name generation process
  1209. may generate zero files.  If it is not set, then a pattern which does
  1210. not match any files will be replaced by a quoted version of the pattern.
  1211. .in -0.4i
  1212. .sp
  1213. The third use of the set command is to set the values of the shell's
  1214. positional parameters to the specified
  1215. .IR args .
  1216. To change the positional parameters without changing any options,
  1217. use ``\fB--\fR'' as the first argument to
  1218. .IR set .
  1219. If no args are present, the set command will leave the value of the
  1220. positional parameters unchanged, so to set the positional parameters
  1221. to set of values that may be empty, execute the command
  1222. .d
  1223. shift $#
  1224. .e
  1225. first to clear out the old values of the positional parameters.
  1226. .b setvar
  1227. .I variable
  1228. .I value
  1229. .br
  1230. Assigns
  1231. .I value
  1232. to
  1233. .IR variable .
  1234. (In general it is better to write
  1235. .I variable=value
  1236. rather than using
  1237. .IR setvar .
  1238. .I Setvar
  1239. is intended to be used in functions that assign values to variables whose
  1240. names are passed as parameters.)
  1241. .b shift
  1242. [
  1243. .I n
  1244. ]
  1245. .br
  1246. Shift the positional parameters
  1247. .I n
  1248. times.
  1249. A shift sets the value of $1 to the value of $2, the value of $2 to
  1250. the value of $3, and so on, decreasing the value of $# by one.
  1251. If there are zero positional parameters, shifting doesn't do anything.
  1252. .b trap
  1253. [
  1254. .I action
  1255. ]
  1256. .IR signal ...
  1257. .br
  1258. Cause the shell to parse and execute
  1259. .I action
  1260. when any of the specified signals are received.
  1261. The signals are specified by signal number.
  1262. .I Action
  1263. may be null or omitted;
  1264. the former causes the specified signal to be ignored and the latter
  1265. causes the default action to be taken.
  1266. When the shell forks off a subshell, it resets trapped (but not ignored)
  1267. signals to the default action.
  1268. The trap command has no effect on signals that were ignored on entry
  1269. to the shell.
  1270. .b umask
  1271. [
  1272. .I mask
  1273. ]
  1274. .br
  1275. Set the value of umask (see
  1276. .IR umask (2))
  1277. to the specified octal value.  If the argument is omitted, the umask
  1278. value is printed.
  1279. .b unset
  1280. .IR name ...
  1281. .br
  1282. The specified variables and functions are unset and unexported.
  1283. If a given name corresponds to both a variable and a function, both the
  1284. variable and the function are unset.
  1285. .b wait
  1286. [
  1287. .I job
  1288. ]
  1289. .br
  1290. Wait for the specified job to complete and return the exit status of the
  1291. last process in the job.  If the argument is omitted, wait for all jobs
  1292. to complete and the return an exit status of zero.
  1293. .SH EXAMPLES
  1294. The following function redefines the \fIcd\fR command:
  1295. .d
  1296. cd() {
  1297.     if bltin cd "$@"
  1298.     then    if test -f .enter
  1299.         then    . .enter
  1300.         else    return 0
  1301.         fi
  1302.     fi
  1303. }
  1304. .e
  1305. This function causes the file ``.enter'' to be read when you enter a
  1306. directory, if it exists.  The \fIbltin\fR command is used to access the
  1307. real \fIcd\fR command.  The ``return 0'' ensures that the function will
  1308. return an exit status of zero if it successfully changes to a directory
  1309. that does not contain a ``.enter'' file.  Redefining existing commands
  1310. is not always a good idea, but this example shows that you can do it if
  1311. you want to.
  1312. .PP
  1313. The suspend function distributed with
  1314. .I ash
  1315. looks like
  1316. .d
  1317. # Copyright (C) 1989 by Kenneth Almquist.  All rights reserved.
  1318. # This file is part of ash, which is distributed under the terms
  1319. # specified by the Ash General Public License.
  1320.  
  1321. suspend() {
  1322.     local -
  1323.     set +j
  1324.     kill -TSTP 0
  1325. }
  1326. .e
  1327. This turns off job control and then sends a stop signal to the current
  1328. process group, which suspends the shell.  (When job control is turned
  1329. on, the shell ignores the TSTP signal.)  Job control will be turned back
  1330. on when the function returns because ``-'' is local to the function.
  1331. As an example of what \fInot\fR to do, consider an earlier version of
  1332. \fIsuspend\fR:
  1333. .d
  1334. suspend() {
  1335.     suspend_flag=$-
  1336.     set +j
  1337.     kill -TSTP 0
  1338.     set -$suspend_flag
  1339. }
  1340. .e
  1341. There are two problems with this.  First, \fBsuspend_flag\fR is a global
  1342. variable rather than a local one, which will cause problems in the
  1343. (unlikely) circumstance that the user is using that variable for some
  1344. other purpose.  Second, consider what happens if shell received an interrupt
  1345. signal after it executes the first \fIset\fR command but before it executes
  1346. the second one.  The interrupt signal will abort the shell function, so
  1347. that the second \fIset\fR command will never be executed and job control
  1348. will be left off.  The first version of \fIsuspend\fR avoids this problem
  1349. by turning job control off only in a local copy of the shell options.  The
  1350. local copy of the shell options is discarded when the function is terminated,
  1351. no matter how it is terminated.
  1352. .SH HINTS
  1353. Shell variables can be used to provide abbreviations for things which
  1354. you type frequently.  For example, I set
  1355. .br
  1356. \h'1i'export h=$HOME
  1357. .br
  1358. in my .profile so that I can type the name of my home directory simply
  1359. by typing ``$h''.
  1360. .PP
  1361. When writing shell procedures, try not to make assumptions about what is
  1362. imported from the environment.  Explicitly unset or initialize all variables,
  1363. rather than assuming they will be unset.  If you use cd, it is a good idea
  1364. to unset CDPATH.
  1365. .PP
  1366. People sometimes use ``<&-'' or ``>&-'' to provide no input to a command
  1367. or to discard the output of a command.  A better way to do this is
  1368. to redirect the input or output of the command to
  1369. .BR /dev/null .
  1370. .PP
  1371. Word splitting and file name generation are performed by default,
  1372. and you have to explicitly use double quotes to suppress it.  This is
  1373. backwards, but you can learn to live with it.  Just get in the habit of
  1374. writing double quotes around variable and command substitutions, and
  1375. omit them only when you really want word splitting and file name generation.
  1376. If you want word splitting but not file name generation, use the
  1377. .B -f
  1378. option.
  1379. .SH AUTHORS
  1380. Kenneth Almquist
  1381. .SH "SEE ALSO"
  1382. echo(1), expr(1), line(1), pwd(1), true(1).
  1383. .SH BUGS
  1384. When command substitution occurs inside a here document, the commands inside
  1385. the here document are run with their standard input closed.  For example,
  1386. the following will not word because the standard input of the
  1387. .I line
  1388. command will be closed when the command is run:
  1389. .d
  1390. cat <<-!
  1391. Line 1: $(line)
  1392. Line 2: $(line)
  1393. !
  1394. .e
  1395. .PP
  1396. Unsetting a function which is currently being executed may cause strange
  1397. behavior.
  1398. .PP
  1399. The shell syntax allows a here document to be terminated by an end of file
  1400. as well as by a line containing the terminator word which follows the ``<<''.
  1401. What this means is that if you mistype the terminator line, the shell
  1402. will silently swallow up the rest of your shell script and stick it
  1403. in the here document.
  1404. EOF
  1405. if test `wc -c < ash.1` -ne 35111
  1406. then    echo 'ash.1 is the wrong size'
  1407. fi
  1408. echo Archive 1 unpacked
  1409. exit
  1410.  
  1411.