old-www/LDP/GNU-Linux-Tools-Summary/html/x2563.htm

258 lines
3.7 KiB
HTML

<HTML
><HEAD
><TITLE
>Command Substitution</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GNU/Linux Command-Line Tools Summary"
HREF="book1.htm"><LINK
REL="UP"
TITLE="Directing Input/Output"
HREF="c2269.htm"><LINK
REL="PREVIOUS"
TITLE="Usage"
HREF="x2361.htm"><LINK
REL="NEXT"
TITLE="Performing more than one command"
HREF="x2622.htm"></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"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x2361.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 6. Directing Input/Output</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x2622.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="COMMAND-SUBSTITUTION"
></A
>Command Substitution</H1
><P
>Command substitution<A
NAME="AEN2566"
></A
> is basically another way to do a pipe,<A
NAME="AEN2568"
></A
> you can use pipes and command substitution interchangeably,<A
NAME="AEN2570"
></A
> it's up to you which one you find easier...</P
><P
>Command substitution can be done in two distinct <A
NAME="AEN2573"
></A
>ways.</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
></DT
><DD
><P
>&nbsp;</P
></DD
><DT
>Method&nbsp;One&nbsp;(back-quotes)<A
NAME="COMMAND-SUBSTITUTION-BACK-QUOTES"
></A
></DT
><DD
><P
>&nbsp;</P
><P
>Simply type:</P
><PRE
CLASS="SCREEN"
>command_1 `command_2 -options`</PRE
><P
>This will execute <A
NAME="AEN2588"
></A
>&ldquo;command_2&rdquo; and it's output <A
NAME="AEN2590"
></A
>will become the input to &ldquo;command_1&rdquo;.</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Backquote key: </B
>The back-quote key is usually located at the same place as the tilde, above the [Tab] key.</P
></BLOCKQUOTE
></DIV
></DD
><DT
>Method&nbsp;Two&nbsp;(dollars&nbsp;sign)<A
NAME="COMMAND-SUBSTITUTION-DOLLARS-SIGN"
></A
></DT
><DD
><P
>&nbsp;</P
><P
>Simply type:</P
><PRE
CLASS="SCREEN"
>command_1 $(command_2)</PRE
><P
>This will execute &ldquo;command_2&rdquo; and it's output will become the input to &ldquo;command_1&rdquo;.</P
></DD
><DT
>Using&nbsp;the&nbsp;pipe&nbsp;instead<A
NAME="COMMAND-SUBSTITUTION-PIPE"
></A
></DT
><DD
><P
>&nbsp;</P
><P
>You can of course use pipes <A
NAME="AEN2609"
></A
>to do the same thing, if you don't know what a pipe <A
NAME="AEN2611"
></A
>is, please see <A
HREF="x2361.htm"
>the Section called <I
>Usage</I
></A
>. For example instead of doing:</P
><PRE
CLASS="SCREEN"
>less $cat file1.txt file2.txt</PRE
><P
>You could do:</P
><PRE
CLASS="SCREEN"
>cat file1.txt file2.txt | less</PRE
><P
>And end up with exactly <A
NAME="AEN2618"
></A
>the same result,<A
NAME="AEN2620"
></A
> it's up to you which way you find easier.</P
></DD
></DL
></DIV
></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="x2361.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x2622.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Usage</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c2269.htm"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Performing more than one command</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>