<story><title>Unix tricks</title><url>http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt</url></story><parent_chain><item><author>unfletch</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &apos;!!:n&apos; selects the nth argument of the last command, and &apos;!$&apos; the last arg
&lt;/code&gt;&lt;/pre&gt;
A lot of people know about &quot;!$&quot; (which is shorthand for !!:$), but that&apos;s just the tip of Bash&apos;s history expansion. I use these things all the time. One of my favorite keystroke savers is adding :h, the head modifier, to !$. For example:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ cp file.txt /some/annoyingly/deep/target/directory/other.txt
    $ cd !$:h
    $ pwd # =&amp;#62; /some/annoyingly/deep/target/directory
&lt;/code&gt;&lt;/pre&gt;
Once you understand how each component works it&apos;s easier to put them together into new (to you) combinations. For example, once you know that !$ is shorthand for !!:$, it&apos;s not a huge leap to reason out that you can use !-2:$ to get the last argument to the 2nd-to-last command. Or !ls:$ for the last arg to the most recent `ls` command.&lt;p&gt;I also prefer to do substitution with the :s modifier rather than ^ as suggested at the link, for consistency&apos;s sake:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ echo &quot;foo bar&quot;
    foo bar
    $ echo !!:s/bar/baz
    foo baz
    $ echo !?bar?:s/foo/qux
    qux bar
&lt;/code&gt;&lt;/pre&gt;
Relevant Bash manual pages:&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Event-Desi...&lt;/a&gt;&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Word-Designators.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Word-Desig...&lt;/a&gt;&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Modifiers.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Modifiers....&lt;/a&gt;</text></item></parent_chain><comment><author>pjungwir</author><text>Another nice ending is `:p` to print the command instead of executing it. I use this if I&apos;m doing something complicated and I want to make sure it&apos;s right. Or if I&apos;m saying `!-n:foo` with n&amp;#62;2. Then just up-arrow and enter to run it for real.</text></comment>
<story><title>Unix tricks</title><url>http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt</url></story><parent_chain><item><author>unfletch</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &apos;!!:n&apos; selects the nth argument of the last command, and &apos;!$&apos; the last arg
&lt;/code&gt;&lt;/pre&gt;
A lot of people know about &quot;!$&quot; (which is shorthand for !!:$), but that&apos;s just the tip of Bash&apos;s history expansion. I use these things all the time. One of my favorite keystroke savers is adding :h, the head modifier, to !$. For example:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ cp file.txt /some/annoyingly/deep/target/directory/other.txt
    $ cd !$:h
    $ pwd # =&amp;#62; /some/annoyingly/deep/target/directory
&lt;/code&gt;&lt;/pre&gt;
Once you understand how each component works it&apos;s easier to put them together into new (to you) combinations. For example, once you know that !$ is shorthand for !!:$, it&apos;s not a huge leap to reason out that you can use !-2:$ to get the last argument to the 2nd-to-last command. Or !ls:$ for the last arg to the most recent `ls` command.&lt;p&gt;I also prefer to do substitution with the :s modifier rather than ^ as suggested at the link, for consistency&apos;s sake:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ echo &quot;foo bar&quot;
    foo bar
    $ echo !!:s/bar/baz
    foo baz
    $ echo !?bar?:s/foo/qux
    qux bar
&lt;/code&gt;&lt;/pre&gt;
Relevant Bash manual pages:&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Event-Desi...&lt;/a&gt;&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Word-Designators.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Word-Desig...&lt;/a&gt;&lt;p&gt;&lt;a href=&quot;http://www.gnu.org/software/bash/manual/html_node/Modifiers.html&quot; rel=&quot;nofollow&quot;&gt;http://www.gnu.org/software/bash/manual/html_node/Modifiers....&lt;/a&gt;</text></item></parent_chain><comment><author>sciurus</author><text>A sometimes handy addition to the s modifier is g, which replaces all instances of the pattern instead of just the first.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ echo &quot;foo foo&quot;
    foo foo
    $ echo !!:s/foo/bar
    echo echo &quot;bar foo&quot;
    echo bar foo

    $ echo &quot;foo foo&quot;
    foo foo
    $ echo !!:gs/foo/bar
    echo echo &quot;bar bar&quot;
    echo bar bar&lt;/code&gt;&lt;/pre&gt;</text></comment>