old-www/LDP/GNU-Linux-Tools-Summary/html/command-substitution.html

301 lines
4.3 KiB
HTML
Raw Permalink Normal View History

2020-08-23 10:33:19 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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="index.html"><LINK
REL="UP"
TITLE="Directing Input/Output"
HREF="directing-input-ouput.html"><LINK
REL="PREVIOUS"
TITLE="Usage"
HREF="usage-input-output.html"><LINK
REL="NEXT"
TITLE="Performing more than one command"
HREF="performing-more-than-one-command.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"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="usage-input-output.html"
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="performing-more-than-one-command.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="COMMAND-SUBSTITUTION"
></A
>6.3. Command Substitution</H1
><P
>Command substitution is basically another way to do a pipe, you can use pipes and command substitution interchangeably, it's up to you which one you find easier...</P
><P
>Command substitution can be done in two distinct ways.</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
></DT
><DD
><P
><3E></P
></DD
><DT
>Method<6F>One<6E>(back-quotes)<A
NAME="COMMAND-SUBSTITUTION-BACK-QUOTES"
></A
></DT
><DD
><P
><3E></P
><P
>Simply type:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>command_1 `command_2 -options`</PRE
></FONT
></TD
></TR
></TABLE
><P
>This will execute &#8220;command_2&#8221; and it's output will become the input to &#8220;command_1&#8221;.</P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Backquote key</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The back-quote key is usually located at the same place as the tilde, above the [Tab] key.</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
>Method<6F>Two<77>(dollars<72>sign)<A
NAME="COMMAND-SUBSTITUTION-DOLLARS-SIGN"
></A
></DT
><DD
><P
><3E></P
><P
>Simply type:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>command_1 $(command_2)</PRE
></FONT
></TD
></TR
></TABLE
><P
>This will execute &#8220;command_2&#8221; and it's output will become the input to &#8220;command_1&#8221;.</P
></DD
><DT
>Using<6E>the<68>pipe<70>instead<A
NAME="COMMAND-SUBSTITUTION-PIPE"
></A
></DT
><DD
><P
><3E></P
><P
>You can of course use pipes to do the same thing, if you don't know what a pipe is, please see <A
HREF="usage-input-output.html"
>Section 6.2</A
>. For example instead of doing:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>less $cat file1.txt file2.txt</PRE
></FONT
></TD
></TR
></TABLE
><P
>You could do:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>cat file1.txt file2.txt | less</PRE
></FONT
></TD
></TR
></TABLE
><P
>And end up with exactly the same result, 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="usage-input-output.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="performing-more-than-one-command.html"
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="directing-input-ouput.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Performing more than one command</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>