old-www/HOWTO/Bash-Prompt-HOWTO/x264.html

236 lines
3.6 KiB
HTML

<HTML
><HEAD
><TITLE
>PROMPT_COMMAND</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Prompt HOWTO"
HREF="index.html"><LINK
REL="UP"
TITLE="External Commands"
HREF="c262.html"><LINK
REL="PREVIOUS"
TITLE="External Commands"
HREF="c262.html"><LINK
REL="NEXT"
TITLE="External Commands in the Prompt"
HREF="x279.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Prompt HOWTO: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="c262.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 4. External Commands</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x279.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN264"
></A
>4.1. PROMPT_COMMAND</H1
><P
>Bash provides an environment variable called <TT
CLASS="ENVAR"
>PROMPT_COMMAND</TT
>.
The contents of this variable are executed as a regular Bash command just
before Bash displays a prompt.&#13;</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>[21:55:01][giles@nikola:~] PS1="[\u@\h:\w]\$ "
[giles@nikola:~] PROMPT_COMMAND="date +%H%M"
2155
[giles@nikola:~] d
bin mail
2156
[giles@nikola:~] </PRE
></FONT
></TD
></TR
></TABLE
><P
>What happened above was that I changed PS1 to no longer include the
<TT
CLASS="USERINPUT"
><B
>\t</B
></TT
> escape sequence (added in a previous section), so
the time was no longer a part of the prompt. Then I used <TT
CLASS="USERINPUT"
><B
>date
+%H%M</B
></TT
> to display the time in a format I like better. But it
appears on a different line than the prompt. Tidying this up using
<TT
CLASS="USERINPUT"
><B
>echo -n ...</B
></TT
> as shown below works with Bash 2.0+, but
appears not to work with Bash 1.14.7: apparently the prompt is drawn in a
different way, and the following method results in overlapping text.&#13;</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>2156
[giles@nikola:~] PROMPT_COMMAND="echo -n [$(date +%H%M)]"
[2156][giles@nikola:~]$
[2156][giles@nikola:~]$ d
bin mail
[2157][giles@nikola:~]$ unset PROMPT_COMMAND
[giles@nikola:~]</PRE
></FONT
></TD
></TR
></TABLE
><P
><TT
CLASS="USERINPUT"
><B
>echo -n ...</B
></TT
> controls the output of the
<B
CLASS="COMMAND"
>date</B
> command and suppresses the trailing newline,
allowing the prompt to appear all on one line. At the end, I used the
<B
CLASS="COMMAND"
>unset</B
> command to remove the
<TT
CLASS="ENVAR"
>PROMPT_COMMAND</TT
> environment variable.&#13;</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="c262.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x279.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>External Commands</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c262.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>External Commands in the Prompt</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>