<!--#include virtual="HTML_begin.inc"-->
  <title>Bash Tricks</title>
  <meta name="keywords" content="Linux, FreeBSD, Plan 9"/>
  <meta name="keywords" content="BASH, fuction definition, \
    command substitution"/>
<!--#include virtual="HTML_head_end.inc"-->
<!--#include virtual="Computer_tabs.inc"-->

<div id="content">
<h2>BASH Tricks</h2>

<p>The following are some bash usage examples. They are not organized because
they are meant to be accessed by links from other documentation. Think of them
as a collection of footnotes.</p>

<hr />

    <a id="commandsubstitution"></a><h3>Command substitution.</h3>

    <p><tt>`<i>command</i>`</tt> or <tt>$(<i>command</i>)</tt> substitutes the
    standard output of <tt><i>command</i></tt>. In the following example the
    process with the pid found in the file <tt>/var/run/ppp0.pid</tt> is killed
    if the process id was placed in the file.</p>

    <pre>    kill `cat /var/run/ppp0.pid`</pre>

    <p>The next example demonstrates how to substitute the current kernel
    version number into a path:</p>

    <pre>    /lib/modules/<b>`uname -r`</b>/modules.dep</pre>

    <p>The current kernel version is substituted for <tt>`uname -r`</tt> and
    the path to modules.dep is completed. The resulting path would look
    something like this:</p>

    <pre>    /lib/modules/<b>2.2.16</b>/modules.dep</pre>

    <p>(Note: ` and ' are different characters.  The character ' will not work
    for command substitution.)</p> <hr />

    <a id="function"></a><h3>Function definitions</h3>

    <p><tt>function <i>name</i> () { <i>list</i>; }</tt> or <tt><i>name</i> ()
    { <i>list</i>; }</tt> defines a function called <tt><i>name</i></tt> and
    will execute whatever is in <tt><i>list</i></tt>. If you want to define a
    function to view the contents of a tar ball called myfile.tar.gz type the
    following</p>

    <pre>    tart () { tar tzvf $1; }</pre>

    <p>at the command line; then type &ldquo;<tt>tart myfile.tar.gz</tt>&rdquo;
    and you will have a list of the contents of the tar ball.  To have the
    function available all the time simply add the definition to your
    <tt>~/.profile</tt> or <tt>~/.bashrc</tt> file.</p>

</div>

<p class="lastupdate">Last updated: <!--#echo var="LAST_MODIFIED" --></p>

<!--#include virtual="HTML_end.inc"-->
