📄 sh.1
字号:
.\" $NetBSD: sh.1,v 1.78 2004/06/03 19:54:37 hubertf Exp $.\" Copyright (c) 1991, 1993.\" The Regents of the University of California. All rights reserved..\".\" This code is derived from software contributed to Berkeley by.\" Kenneth Almquist..\".\" 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..\".\" @(#)sh.1 8.6 (Berkeley) 5/4/95.\".Dd April 17, 2004.Os.Dt SH 1.Sh NAME.Nm sh.Nd command interpreter (shell).Sh SYNOPSIS.Nm.Bk -words.Op Fl aCefnuvxIimqVEb.Op Cm +aCefnuvxIimqVEb.Ek.Bk -words.Op Fl o Ar option_name.Op Cm +o Ar option_name.Ek.Bk -words.Op Ar command_file Oo Ar argument ... Oc.Ek.Nm.Fl c.Bk -words.Op Fl aCefnuvxIimqVEb.Op Cm +aCefnuvxIimqVEb.Ek.Bk -words.Op Fl o Ar option_name.Op Cm +o Ar option_name.Ek.Bk -words.Ar command_string.Op Ar command_name Oo Ar argument ... Oc.Ek.Nm.Fl s.Bk -words.Op Fl aCefnuvxIimqVEb.Op Cm +aCefnuvxIimqVEb.Ek.Bk -words.Op Fl o Ar option_name.Op Cm +o Ar option_name.Ek.Bk -words.Op Ar argument ....Ek.Sh DESCRIPTION.Nmis the standard command interpreter for the system.The current version of.Nmis in the process of being changed to conform with the.Tn POSIX1003.2 and 1003.2a specifications for the shell.This version has manyfeatures which make it appear similar in some respects to the Korn shell,but it is not a Korn shell clone (see.Xr ksh 1 ) .Only features designated by.Tn POSIX ,plus a few Berkeley extensions, are being incorporated into this shell..\" We expect.\" .Tn POSIX.\" conformance by the time 4.4 BSD is released.This man page is not intendedto be a tutorial or a complete specification of the shell..Ss OverviewThe shell is a command that reads lines from either a file or theterminal, interprets them, and generally executes other commands.It is the program that is running when a user logs into the system(although a user can select a different shell with the.Xr chsh 1command).The shell implements a language that has flow controlconstructs, a macro facility that provides a variety of features inaddition to data storage, along with built in history and line editingcapabilities.It incorporates many features to aid interactive use andhas the advantage that the interpretative language is common to bothinteractive and non-interactive use (shell scripts).That is, commandscan be typed directly to the running shell or can be put into a file andthe file can be executed directly by the shell..Ss InvocationIf no args are present and if the standard input of the shellis connected to a terminal (or if the.Fl iflag is set),and the.Fl coption is not present, the shell is considered an interactive shell.An interactive shell generally prompts before each command and handlesprogramming and command errors differently (as described below).When first starting,the shell inspects argument 0, and if it begins with a dash.Sq - ,the shell is also considereda login shell.This is normally done automatically by the systemwhen the user first logs in.A login shell first reads commandsfrom the files.Pa /etc/profileand.Pa .profileif they exist.If the environment variable.Ev ENVis set on entry to a shell, or is set in the.Pa .profileof a login shell, the shell next readscommands from the file named in.Ev ENV .Therefore, a user should place commands that are to be executed only atlogin time in the.Pa .profilefile, and commands that are executed for every shell inside the.Ev ENVfile.To set the.Ev ENVvariable to some file, place the following line in your.Pa .profileof your home directory.Pp.Dl ENV=$HOME/.shinit; export ENV.Ppsubstituting for.Dq .shinitany filename you wish.Since the.Ev ENVfile is read for every invocation of the shell, including shell scriptsand non-interactive shells, the following paradigm is useful forrestricting commands in the.Ev ENVfile to interactive invocations.Place commands within the.Dq caseand.Dq esacbelow (these commands are described later):.Pp.Bl -item -compact -offset indent.It.Li case $- in *i*).Bl -item -compact -offset indent.It.Li # commands for interactive use only.It.Li ....El.It.Li esac.El.PpIf command line arguments besides the options have been specified, thenthe shell treats the first argument as the name of a file from which toread commands (a shell script), and the remaining arguments are set as thepositional parameters of the shell ($1, $2, etc).Otherwise, the shellreads commands from its standard input..Ss Argument List ProcessingAll of the single letter options have a corresponding name that can beused as an argument to the.Fl ooption.The set.Fl oname is provided next to the single letter option inthe description below.Specifying a dash.Dq -turns the option on, while using a plus.Dq +disables the option.The following options can be set from the command line orwith the.Ic setbuiltin (described later)..Bl -tag -width aaaallexportfoo -offset indent.It Fl a Em allexportExport all variables assigned to..It Fl cRead commands from the.Ar command_stringoperand instead of from the standard input.Special parameter 0 will be set from the.Ar command_nameoperand and the positional parameters ($1, $2, etc.)set from the remaining argument operands..It Fl C Em noclobberDon't overwrite existing files with.Dq \*[Gt] ..It Fl e Em errexitIf not interactive, exit immediately if any untested command fails.The exit status of a command is considered to beexplicitly tested if the command is used to control an.Ic if ,.Ic elif ,.Ic while ,or.Ic until ;or if the command is the left hand operand of an.Dq \*[Am]\*[Am]or.Dq ||operator..It Fl f Em noglobDisable pathname expansion..It Fl n Em noexecIf not interactive, read commands but do not execute them.This is useful for checking the syntax of shell scripts..It Fl u Em nounsetWrite a message to standard error when attempting to expand a variablethat is not set, and if the shell is not interactive, exit immediately..It Fl v Em verboseThe shell writes its input to standard error as it is read.Useful for debugging..It Fl x Em xtraceWrite each command to standard error (preceded by a.Sq +\ )before it is executed.Useful for debugging..It Fl q Em quietprofileIf the.Fl vor.Fl xoptions have been set, do not apply them when readinginitialization files, these being.Pa /etc/profile ,.Pa .profile ,and the file specified by the.Ev ENVenvironment variable..It Fl I Em ignoreeofIgnore EOF's from input when interactive..It Fl i Em interactiveForce the shell to behave interactively..It Fl m Em monitorTurn on job control (set automatically when interactive)..It Fl s Em stdinRead commands from standard input (set automatically if no file argumentsare present).This option has no effect when set after the shell hasalready started running (i.e. with.Ic set ) ..It Fl V Em viEnable the built-in.Xr vi 1command line editor (disables.Fl Eif it has been set).(See the.Sx Command Line Editingsection below.).It Fl E Em emacsEnable the built-in emacs stylecommand line editor (disables.Fl Vif it has been set).(See the.Sx Command Line Editingsection below.).It Fl b Em notifyEnable asynchronous notification of background job completion.(UNIMPLEMENTED for 4.4alpha).It "\ \ " Em cdprintMake an interactive shell always print the new directory name whenchanged by the.Ic cdcommand..El.Ss Lexical StructureThe shell reads input in terms of lines from a file and breaks it up intowords at whitespace (blanks and tabs), and at certain sequences ofcharacters that are special to the shell called.Dq operators .There are two types of operators: control operators and redirectionoperators (their meaning is discussed later).Following is a list of operators:.Bl -ohang -offset indent.It "Control operators:".Dl \*[Am] \*[Am]\*[Am] \&( \&) \&; ;; | || \*[Lt]newline\*[Gt].It "Redirection operators:".Dl \*[Lt] \*[Gt] \*[Gt]| \*[Lt]\*[Lt] \*[Gt]\*[Gt] \*[Lt]\*[Am] \*[Gt]\*[Am] \*[Lt]\*[Lt]- \*[Lt]\*[Gt].El.Ss QuotingQuoting is used to remove the special meaning of certain characters orwords to the shell, such as operators, whitespace, or keywords.There are three types of quoting: matched single quotes,matched double quotes, and backslash..Ss BackslashA backslash preserves the literal meaning of the followingcharacter, with the exception of.Aq newline .A backslash preceding a.Aq newlineis treated as a line continuation..Ss Single QuotesEnclosing characters in single quotes preserves the literal meaning of allthe characters (except single quotes, making it impossible to putsingle-quotes in a single-quoted string)..Ss Double QuotesEnclosing characters within double quotes preserves the literalmeaning of all characters except dollarsign.Pq $ ,backquote.Pq ` ,and backslash.Pq \e .The backslash inside double quotes is historically weird, and serves toquote only the following characters:.Dl $ ` \*q \e \*[Lt]newline\*[Gt] .Otherwise it remains literal..Ss Reserved WordsReserved words are words that have special meaning to theshell and are recognized at the beginning of a line andafter a control operator.The following are reserved words:.Bl -column while while while while while -offset indent.It ! Ta elif Ta fi Ta while Ta case.It else Ta for Ta then Ta { Ta }.It do Ta done Ta until Ta if Ta esac.El.PpTheir meaning is discussed later..Ss AliasesAn alias is a name and corresponding value set using the.Ic aliasbuiltin command.Whenever a reserved word may occur (see above),and after checking for reserved words, the shellchecks the word to see if it matches an alias.If it does, it replaces it in the input stream with its value.For example, if there is an alias called.Dq lfwith the value.Dq "ls -F" ,then the input:.Pp.Dl lf foobar Aq return.Ppwould become.Pp.Dl ls -F foobar Aq return.PpAliases provide a convenient way for naive users to create shorthands forcommands without having to learn how to create functions with arguments.They can also be used to create lexically obscure code.This use is discouraged..Ss CommandsThe shell interprets the words it reads according to a language, thespecification of which is outside the scope of this man page (refer to theBNF in the.Tn POSIX1003.2 document).Essentially though, a line is read and if the firstword of the line (or after a control operator) is not a reserved word,then the shell has recognized a simple command.Otherwise, a complexcommand or some other special construct may have been recognized..Ss Simple CommandsIf a simple command has been recognized, the shell performsthe following actions:.Bl -enum -offset indent.ItLeading words of the form.Dq name=valueare stripped off and assigned to the environment of the simple command.Redirection operators and their arguments (as described below) arestripped off and saved for processing..ItThe remaining words are expanded as described inthe section called.Dq Expansions ,and the first remaining word is considered the command name and thecommand is located.The remaining words are considered the arguments of the command.If no command name resulted, then the.Dq name=valuevariable assignments recognized in item 1 affect the current shell..ItRedirections are performed as described in the next section..El.Ss RedirectionsRedirections are used to change where a command reads its input or sendsits output.In general, redirections open, close, or duplicate anexisting reference to a file.The overall format used for redirection is:.Pp.Dl [n] Va redir-op Ar file.Ppwhere.Va redir-opis one of the redirection operators mentioned previously.Following is a list of the possible redirections.The.Bq nis an optional number, as in.Sq 3(not.Sq Bq 3 ) ,that refers to a file descriptor..Bl -tag -width aaabsfiles -offset indent.It [n] Ns \*[Gt] fileRedirect standard output (or n) to file..It [n] Ns \*[Gt]| fileSame, but override the.Fl Coption..It [n] Ns \*[Gt]\*[Gt] fileAppend standard output (or n) to file..It [n] Ns \*[Lt] fileRedirect standard input (or n) from file..It [n1] Ns \*[Lt]\*[Am] Ns n2Duplicate standard input (or n1) from file descriptor n2..It [n] Ns \*[Lt]\*[Am]-Close standard input (or n)..It [n1] Ns \*[Gt]\*[Am] Ns n2Duplicate standard output (or n1) to n2..It [n] Ns \*[Gt]\*[Am]-Close standard output (or n)..It [n] Ns \*[Lt]\*[Gt] fileOpen file for reading and writing on standard input (or n)..El.PpThe following redirection is often called a.Dq here-document ..Bl -item -offset indent.It.Li [n]\*[Lt]\*[Lt] delimiter.Dl here-doc-text ....Li delimiter.El.PpAll the text on successive lines up to the delimiter is saved away andmade available to the command on standard input, or file descriptor n ifit is specified.If the delimiter as specified on the initial line isquoted, then the here-doc-text is treated literally, otherwise the text issubjected to parameter expansion, command substitution, and arithmeticexpansion (as described in the section on.Dq Expansions ) .If the operator is.Dq \*[Lt]\*[Lt]-instead of.Dq \*[Lt]\*[Lt] ,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -