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

193 lines
3.0 KiB
HTML

<HTML
><HEAD
><TITLE
>Checking the Current TTY</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="Prompt Code Snippets"
HREF="c679.html"><LINK
REL="PREVIOUS"
TITLE="Total Bytes in the Current Directory"
HREF="x711.html"><LINK
REL="NEXT"
TITLE="Stopped Jobs Count"
HREF="x733.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="x711.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Prompt Code Snippets</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x733.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN721"
></A
>11.5. Checking the Current TTY</H1
><P
>The <TT
CLASS="USERINPUT"
><B
>tty</B
></TT
> command returns the filename of the
terminal connected to standard input. This comes in two formats on the
Linux systems I have used, either "/dev/tty4" or "/dev/pts/2". I've used
several methods over time, but the simplest I've found so far (probably
both Linux- and Bash-2.x specific) is <TT
CLASS="USERINPUT"
><B
>temp=$(tty) ; echo
${temp:5}</B
></TT
>. This removes the first five characters of the
<TT
CLASS="USERINPUT"
><B
>tty</B
></TT
> output, in this case "/dev/".&#13;</P
><P
>Previously, I used <TT
CLASS="USERINPUT"
><B
>tty | sed -e "s:/dev/::"</B
></TT
>, which
removes the leading "/dev/". Older systems (in my experience, RedHat
through 5.2) returned only filenames in the "/dev/tty4" format, so I used
<TT
CLASS="USERINPUT"
><B
>tty | sed -e "s/.*tty\(.*\)/\1/"</B
></TT
>. &#13;</P
><P
>An alternative method:
<TT
CLASS="USERINPUT"
><B
>ps ax | grep $$ | awk '{ print $2 }'</B
></TT
>.</P
><P
>Relative speed: the ${temp:5} method takes about 0.12 seconds on an
unloaded 486SX25, the sed-driven method takes about 0.19 seconds, the
awk-driven method takes about 0.79 seconds.&#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="x711.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="x733.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Total Bytes in the Current Directory</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c679.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Stopped Jobs Count</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>