📄 variables2.html
字号:
HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>As of version 2.05 of Bash, filename globbing no longer distinguishes between lowercase and uppercase letters in a character range between brackets. For example, <BCLASS="COMMAND">ls [A-M]*</B> would match both <TTCLASS="FILENAME">File1.txt</TT> and <TTCLASS="FILENAME">file1.txt</TT>. To revert to the customary behavior of bracket matching, set <TTCLASS="VARNAME">LC_COLLATE</TT> to <TTCLASS="OPTION">C</TT> by an <TTCLASS="USERINPUT"><B>export LC_COLLATE=C</B></TT> in <TTCLASS="FILENAME">/etc/profile</TT> and/or <TTCLASS="FILENAME">~/.bashrc</TT>.</P></TD></TR></TABLE></DIV></DD><DT><TTCLASS="VARNAME">$LC_CTYPE</TT></DT><DD><P>This internal variable controls character interpretation in <AHREF="globbingref.html">globbing</A> and pattern matching.</P></DD><DT><ANAME="LINENOREF"></A><TTCLASS="VARNAME">$LINENO</TT></DT><DD><P>This variable is the line number of the shell script in which this variable appears. It has significance only within the script in which it appears, and is chiefly useful for debugging purposes.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # *** BEGIN DEBUG BLOCK *** 2 last_cmd_arg=$_ # Save it. 3 4 echo "At line number $LINENO, variable \"v1\" = $v1" 5 echo "Last command argument processed = $last_cmd_arg" 6 # *** END DEBUG BLOCK ***</PRE></TD></TR></TABLE></P></DD><DT><ANAME="MACHTYPEREF"></A><TTCLASS="VARNAME">$MACHTYPE</TT></DT><DD><P>machine type</P><P>Identifies the system hardware.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $MACHTYPE</B></TT> <TTCLASS="COMPUTEROUTPUT">i686</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="OLDPWD"></A><TTCLASS="VARNAME">$OLDPWD</TT></DT><DD><P>old working directory (<SPANCLASS="QUOTE">"OLD-print-working-directory"</SPAN>, previous directory you were in)</P></DD><DT><TTCLASS="VARNAME">$OSTYPE</TT></DT><DD><P>operating system type</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $OSTYPE</B></TT> <TTCLASS="COMPUTEROUTPUT">linux</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="PATHREF"></A><TTCLASS="VARNAME">$PATH</TT></DT><DD><P>path to binaries, usually <TTCLASS="FILENAME">/usr/bin/</TT>, <TTCLASS="FILENAME">/usr/X11R6/bin/</TT>, <TTCLASS="FILENAME">/usr/local/bin</TT>, etc.</P><P>When given a command, the shell automatically does a hash table search on the directories listed in the <ICLASS="FIRSTTERM">path</I> for the executable. The path is stored in the <AHREF="othertypesv.html#ENVREF">environmental variable</A>, <TTCLASS="VARNAME">$PATH</TT>, a list of directories, separated by colons. Normally, the system stores the <TTCLASS="VARNAME">$PATH</TT> definition in <TTCLASS="FILENAME">/etc/profile</TT> and/or <TTCLASS="FILENAME">~/.bashrc</TT> (see <AHREF="files.html">Appendix G</A>).</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><BCLASS="COMMAND">echo $PATH</B> <TTCLASS="COMPUTEROUTPUT">/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin</TT></PRE></TD></TR></TABLE> </P><P><TTCLASS="USERINPUT"><B>PATH=${PATH}:/opt/bin</B></TT> appends the <TTCLASS="FILENAME">/opt/bin</TT> directory to the current path. In a script, it may be expedient to temporarily add a directory to the path in this way. When the script exits, this restores the original <TTCLASS="VARNAME">$PATH</TT> (a child process, such as a script, may not change the environment of the parent process, the shell).</P><P><ANAME="CURRENTWDREF"></A></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The current <SPANCLASS="QUOTE">"working directory"</SPAN>, <TTCLASS="FILENAME">./</TT>, is usually omitted from the <TTCLASS="VARNAME">$PATH</TT> as a security measure.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="PIPESTATUSREF"></A><TTCLASS="VARNAME">$PIPESTATUS</TT></DT><DD><P><AHREF="arrays.html#ARRAYREF">Array</A> variable holding exit status(es) of last executed <ICLASS="FIRSTTERM">foreground</I> <AHREF="special-chars.html#PIPEREF">pipe</A>. Interestingly enough, this does not necessarily give the same result as the <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of the last executed command.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $PIPESTATUS</B></TT> <TTCLASS="COMPUTEROUTPUT">0</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -al | bogus_command</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: bogus_command: command not found</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $PIPESTATUS</B></TT> <TTCLASS="COMPUTEROUTPUT">141</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -al | bogus_command</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: bogus_command: command not found</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $?</B></TT> <TTCLASS="COMPUTEROUTPUT">127</TT> </PRE></TD></TR></TABLE> </P><P>The members of the <TTCLASS="VARNAME">$PIPESTATUS</TT> array hold the exit status of each respective command executed in a pipe. <TTCLASS="VARNAME">$PIPESTATUS[0]</TT> holds the exit status of the first command in the pipe, <TTCLASS="VARNAME">$PIPESTATUS[1]</TT> the exit status of the second command, and so on.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P> The <TTCLASS="VARNAME">$PIPESTATUS</TT> variable may contain an erroneous <SPANCLASS="ERRORCODE">0</SPAN> value in a login shell (in releases prior to 3.0 of Bash). </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">tcsh% </TT><TTCLASS="USERINPUT"><B>bash</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>who | grep nobody | sort</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ${PIPESTATUS[*]}</B></TT> <TTCLASS="COMPUTEROUTPUT">0</TT> </PRE></TD></TR></TABLE> </P><P> The above lines contained in a script would produce the expected <TTCLASS="COMPUTEROUTPUT">0 1 0</TT> output. </P><P> Thank you, Wayne Pollock for pointing this out and supplying the above example. </P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <TTCLASS="VARNAME">$PIPESTATUS</TT> variable gives unexpected results in some contexts.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $BASH_VERSION</B></TT> <TTCLASS="COMPUTEROUTPUT">3.00.14(1)-release</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>$ ls | bogus_command | wc</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: bogus_command: command not found 0 0 0</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ${PIPESTATUS[@]}</B></TT> <TTCLASS="COMPUTEROUTPUT">141 127 0</TT> </PRE></TD></TR></TABLE> </P><P>Chet Ramey attributes the above output to the behavior of <AHREF="external.html#LSREF">ls</A>. If <ICLASS="FIRSTTERM">ls</I> writes to a <ICLASS="FIRSTTERM">pipe</I> whose output is not read, then SIGPIPE kills it, and its <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> is <SPANCLASS="RETURNVALUE">141</SPAN>. Otherwise its exit status is <SPANCLASS="RETURNVALUE">0</SPAN>, as expected. This likewise is the case for <AHREF="textproc.html#TRREF">tr</A>.</P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><TTCLASS="VARNAME">$PIPESTATUS</TT> is a <SPANCLASS="QUOTE">"volatile"</SPAN> variable. It needs to be captured immediately after the pipe in question, before any other command intervenes.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>$ ls | bogus_command | wc</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: bogus_command: command not found 0 0 0</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ${PIPESTATUS[@]}</B></TT> <TTCLASS="COMPUTEROUTPUT">0 127 0</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ${PIPESTATUS[@]}</B></TT> <TTCLASS="COMPUTEROUTPUT">0</TT> </PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <AHREF="bashver3.html#PIPEFAILREF">pipefail option</A> may be useful in cases where <TTCLASS="VARNAME">$PIPESTATUS</TT> does not give the desired information.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="PPIDREF"></A><TTCLASS="VARNAME">$PPID</TT></DT><DD><P></P><P>The <TTCLASS="VARNAME">$PPID</TT> of a process is the process ID (<TTCLASS="VARNAME">pid</TT>) of its parent process. <ANAME="AEN4419"HREF="#FTN.AEN4419">[1]</A> </P><P>Compare this with the <AHREF="system.html#PIDOFREF">pidof</A> command.</P></DD><DT><TTCLASS="VARNAME">$PROMPT_COMMAND</TT></DT><DD><P>A variable holding a command to be executed just before the primary prompt, <TTCLASS="VARNAME">$PS1</TT> is to be displayed.</P></DD><DT><ANAME="PS1REF"></A><TTCLASS="VARNAME">$PS1</TT></DT><DD><P>This is the main prompt, seen at the command line.</P></DD><DT><ANAME="SECPROMPTREF"></A><TTCLASS="VARNAME">$PS2</TT></DT><DD><P>The secondary prompt, seen when additional input is expected. It displays as <SPANCLASS="QUOTE">">"</SPAN>.</P></DD><DT><TTCLASS="VARNAME">$PS3</TT></DT><DD><P>The tertiary prompt, displayed in a <AHREF="testbranch.html#SELECTREF">select</A> loop (see <AHREF="testbranch.html#EX31">Example 10-29</A>).</P></DD><DT><TTCLASS="VARNAME">$PS4</TT></DT><DD><P>The quartenary prompt, shown at the beginning of each line of output when invoking a script with the <SPANCLASS="TOKEN">-x</SPAN> <AHREF="options.html#OPTIONSREF">option</A>. It displays as <SPANCLASS="QUOTE">"+"</SPAN>.</P></DD><DT><ANAME="PWDREF"></A><TTCLASS="VARNAME">$PWD</TT></DT><DD><P>working directory (directory you are in at the time)</P><P>This is the analog to the <AHREF="internal.html#PWD2REF">pwd</A> builtin command.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -