📄 sh.1
字号:
If parameter is unset or null, theexpansion of word (or a message indicating it is unset if word is omitted)is written to standard error and the shell exits with a nonzero exit status.Otherwise, the value of parameter is substituted.An interactive shell need not exit..It ${parameter:+word}Use Alternative Value.If parameter is unset or null, null issubstituted; otherwise, the expansion of word is substituted..El.PpIn the parameter expansions shown previously, use of the colon in theformat results in a test for a parameter that is unset or null; omissionof the colon results in a test for a parameter that is only unset..Bl -tag -width aaparameterwordaaaaa.It ${#parameter}String Length.The length in characters of the value of parameter..El.PpThe following four varieties of parameter expansion provide for substringprocessing.In each case, pattern matching notation (see.Sx Shell Patterns ) ,rather than regular expression notation, is used to evaluate the patterns.If parameter is * or @, the result of the expansion is unspecified.Enclosing the full parameter expansion string in double-quotes does notcause the following four varieties of pattern characters to be quoted,whereas quoting characters within the braces has this effect..Bl -tag -width aaparameterwordaaaaa.It ${parameter%word}Remove Smallest Suffix Pattern.The word is expanded to produce a pattern.The parameter expansion then results in parameter, with thesmallest portion of the suffix matched by the pattern deleted..It ${parameter%%word}Remove Largest Suffix Pattern.The word is expanded to produce a pattern.The parameter expansion then results in parameter, with the largestportion of the suffix matched by the pattern deleted..It ${parameter#word}Remove Smallest Prefix Pattern.The word is expanded to produce a pattern.The parameter expansion then results in parameter, with thesmallest portion of the prefix matched by the pattern deleted..It ${parameter##word}Remove Largest Prefix Pattern.The word is expanded to produce a pattern.The parameter expansion then results in parameter, with the largestportion of the prefix matched by the pattern deleted..El.Ss Command SubstitutionCommand substitution allows the output of a command to be substituted inplace of the command name itself.Command substitution occurs when the command is enclosed as follows:.Pp.Dl $(command).Ppor.Po.Dq backquotedversion.Pc :.Pp.Dl `command`.PpThe shell expands the command substitution by executing command in asubshell environment and replacing the command substitution with thestandard output of the command, removing sequences of one or more.Ao newline Ac Ns sat the end of the substitution.(Embedded.Ao newline Ac Ns sbeforethe end of the output are not removed; however, during field splitting,they may be translated into.Ao space Ac Ns s ,depending on the value of.Ev IFSand quoting that is in effect.).Ss Arithmetic ExpansionArithmetic expansion provides a mechanism for evaluating an arithmeticexpression and substituting its value.The format for arithmetic expansion is as follows:.Pp.Dl $((expression)).PpThe expression is treated as if it were in double-quotes, exceptthat a double-quote inside the expression is not treated specially.The shell expands all tokens in the expression for parameter expansion,command substitution, and quote removal..PpNext, the shell treats this as an arithmetic expression andsubstitutes the value of the expression..Ss White Space Splitting (Field Splitting)After parameter expansion, command substitution, andarithmetic expansion the shell scans the results ofexpansions and substitutions that did not occur in double-quotes forfield splitting and multiple fields can result..PpThe shell treats each character of the.Ev IFSas a delimiter and use the delimiters to split the results of parameterexpansion and command substitution into fields..Ss Pathname Expansion (File Name Generation)Unless the.Fl fflag is set, file name generation is performed after word splitting iscomplete.Each word is viewed as a series of patterns, separated by slashes.The process of expansion replaces the word with the names of allexisting files whose names can be formed by replacing each pattern with astring that matches the specified pattern.There are two restrictions onthis: first, a pattern cannot match a string containing a slash, andsecond, a pattern cannot match a string starting with a period unless thefirst character of the pattern is a period.The next section describes thepatterns used for both Pathname Expansion and the.Ic casecommand..Ss Shell PatternsA pattern consists of normal characters, which match themselves,and meta-characters.The meta-characters are.Dq \&! ,.Dq * ,.Dq \&? ,and.Dq \&[ .These characters lose their special meanings if they are quoted.When command or variable substitution is performedand the dollar sign or back quotes are not double quoted,the value of the variable or the output ofthe command is scanned for these characters and they are turned intometa-characters..PpAn asterisk.Pq Dq *matches any string of characters.A question mark matches any single character.A left bracket.Pq Dq \&[introduces a character class.The end of the character class is indicated by a.Pq Dq \&] ;if the.Dq \&]is missing then the.Dq \&[matches a.Dq \&[rather than introducing a character class.A character class matches any of the characters between the square brackets.A range of characters may be specified using a minus sign.The character class may be complementedby making an exclamation point the first character of the character class..PpTo include a.Dq \&]in a character class, make it the first character listed (after the.Dq \&! ,if any).To include a minus sign, make it the first or last character listed..Ss BuiltinsThis section lists the builtin commands which are builtin because theyneed to perform some operation that can't be performed by a separateprocess.In addition to these, there are several other commands that maybe builtin for efficiency (e.g..Xr printf 1 ,.Xr echo 1 ,.Xr test 1 ,etc)..Bl -tag -width 5n.It :A null command that returns a 0 (true) exit value..It \&. fileThe commands in the specified file are read and executed by the shell..It alias Op Ar name Ns Op Ar "=string ..."If.Ar name=stringis specified, the shell defines the alias.Ar namewith value.Ar string .If just.Ar nameis specified, the value of the alias.Ar nameis printed.With no arguments, the.Ic aliasbuiltin prints thenames and values of all defined aliases (see.Ic unalias ) ..It bg [ Ar job ] ...Continue the specified jobs (or the current job if nojobs are given) in the background..It Xo command.Op Fl p.Op Fl v.Op Fl V.Ar command.Op Ar arg ....XcExecute the specified command but ignore shell functions when searchingfor it.(This is useful when youhave a shell function with the same name as a builtin command.).Bl -tag -width 5n.It Fl psearch for command using a.Ev PATHthat guarantees to find all the standard utilities..It Fl VDo not execute the command butsearch for the command and print the resolution of thecommand search.This is the same as the type builtin..It Fl vDo not execute the command butsearch for the command and print the absolute pathnameof utilities, the name for builtins or the expansion of aliases..El.It cd Op Ar directory Op Ar replaceSwitch to the specified directory (default.Ev $HOME ) .If.Ar replaceis specified, then the new directory name is generated by replacingthe first occurrence of.Ar directoryin the current directory name with.Ar replace .Otherwise if an entry for.Ev CDPATHappears in the environment of the.Ic cdcommand or the shell variable.Ev CDPATHis set and the directory name does not begin with a slash, then thedirectories listed in.Ev CDPATHwill be searched for the specified directory.The format of.Ev CDPATHis the same as that of.Ev PATH .In an interactive shell, the.Ic cdcommand will print out the name of thedirectory that it actually switched to if this is different from the namethat the user gave.These may be different either because the.Ev CDPATHmechanism was used or because a symbolic link was crossed..It eval Ar string ...Concatenate all the arguments with spaces.Then re-parse and execute the command..It exec Op Ar command arg ...Unless command is omitted, the shell process is replaced with thespecified program (which must be a real program, not a shell builtin orfunction).Any redirections on the.Ic execcommand are marked as permanent, so that they are not undone when the.Ic execcommand finishes..It exit Op Ar exitstatusTerminate the shell process.If.Ar exitstatusis given it is used as the exit status of the shell; otherwise theexit status of the preceding command is used..It export Ar name ....It export Fl pThe specified names are exported so that they will appear in theenvironment of subsequent commands.The only way to un-export a variable is to unset it.The shell allows the value of a variable to be set at thesame time it is exported by writing.Pp.Dl export name=value.PpWith no arguments the export command lists the names of all exported variables.With the.Fl poption specified the output will be formatted suitably for non-interactive use..It Xo fc Op Fl e Ar editor.Op Ar first Op Ar last.Xc.It Xo fc Fl l.Op Fl nr.Op Ar first Op Ar last.Xc.It Xo fc Fl s Op Ar old=new.Op Ar first.XcThe.Ic fcbuiltin lists, or edits and re-executes, commands previously enteredto an interactive shell..Bl -tag -width 5n.It Fl e No editorUse the editor named by editor to edit the commands.The editor string is a command name, subject to search via the.Ev PATHvariable.The value in the.Ev FCEDITvariable is used as a default when.Fl eis not specified.If.Ev FCEDITis null or unset, the value of the.Ev EDITORvariable is used.If.Ev EDITORis null or unset,.Xr ed 1is used as the editor..It Fl l No (ell)List the commands rather than invoking an editor on them.The commands are written in the sequence indicated bythe first and last operands, as affected by.Fl r ,with each command preceded by the command number..It Fl nSuppress command numbers when listing with -l..It Fl rReverse the order of the commands listed (with.Fl l )or edited (with neither.Fl lnor.Fl s ) ..It Fl sRe-execute the command without invoking an editor..It first.It lastSelect the commands to list or edit.The number of previous commands thatcan be accessed are determined by the value of the.Ev HISTSIZEvariable.The value of first or last or both are one of the following:.Bl -tag -width 5n.It [+]numberA positive number representing a command number; command numbers can bedisplayed with the.Fl loption..It Fl numberA negative decimal number representing the command that was executednumber of commands previously.For example, \-1 is the immediately previous command..El.It stringA string indicating the most recently entered command that begins withthat string.If the old=new operand is not also specified with.Fl s ,the string form of the first operand cannot contain an embedded equal sign..El.PpThe following environment variables affect the execution of fc:.Bl -tag -width HISTSIZE.It Ev FCEDITName of the editor to use..It Ev HISTSIZEThe number of previous commands that are accessible..El.It fg Op Ar jobMove the specified job or the current job to the foreground..It getopts Ar optstring varThe.Tn POSIX.Ic getoptscommand, not to be confused with the.Em Bell Labs-derived.Xr getopt 1 ..PpThe first argument should be a series of letters, each of which may beoptionally followed by a colon to indicate that the option requires anargument.The variable specified is set to the parsed option..PpThe.Ic getoptscommand deprecates the older.Xr getopt 1utility due to its handling of arguments containing whitespace..PpThe.Ic getoptsbuiltin may be used to obtain options and their argumentsfrom a list of parameters.When invoked,.Ic getoptsplaces the value of the next option from the option string in the list inthe shell variable specified by.Va varand its index in the shell variable.Ev OPTIND .When the shell is invoked,.Ev OPTINDis initialized to 1.For each option that requires an argument, the.Ic getoptsbuiltin will place it in the shell variable.Ev OPTARG .If an option is not allowed for in the.Va optstring ,then.Ev OPTARGwill be unset..Pp.Va optstringis a string of recognized option letters (see.Xr getopt 3 ) .If a letter is followed by a colon, the option is expected to have anargument which may or may not be separated from it by white space.If an option character is not found where expected,.Ic getoptswill set the variable.Va varto a.Dq \&? ;.Ic getoptswill then unset.Ev OPTARGand write output to standard error.By specifying a colon as the first character of.Va optstringall errors will be ignored..PpA nonzero value is returned when the last option is reached.If there are no remaining arguments,.Ic getoptswill set.Va varto the special option,.Dq -- ,otherwise, it will set.Va varto.Dq \&? ..PpThe following code fragment shows how one might process the argumentsfor a command that can take the options.Op aand.Op b ,and the option.Op c ,which requires an argument..Pp.Bd -literal -offset indentwhile getopts abc: fdo case $f in a | b) flag=$f;; c) carg=$OPTARG;; \\?) echo $USAGE; exit 1;; esacdoneshift `expr $OPTIND - 1`.Ed.PpThis code will accept any of the following as equivalent:.Pp.Bd -literal -offset indentcmd \-acarg file filecmd \-a \-c arg file filecmd \-carg -a file filecmd \-a \-carg \-\- file file.Ed.It hash Fl rv Ar command ...The shell maintains a hash table which remembers the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -