News for hackers. Stuff that matters.
Re:The most useful thing in bash by svachalek on Wednesday April 17, 2013 @02:16PM (#5566053)
One history trick I use a lot that most people don't seem to know about is the !$ variable, which refers to the last argument of the previous command. It's basically a bash pronoun. !! is the entire previous command which is occasionally useful with find:

find . -name foo

vi $(!!)

Re:The most useful thing in bash by dllthomas on Wednesday April 17, 2013 @03:06PM (#5566384)
$_ is a variable for the same thing. The only difference winds up being the effect on the current line's result in your history: !$ is replaced while $_ stays $_ and so its meaning will change if you run the same command later, which can be what you want or not, depending.
Re:The most useful thing in bash by scott_karana on Wednesday April 17, 2013 @03:35PM (#5566577)
You're right in this specific case, but there are other useful ! arguments, so it's worth learning both.

It's the "Event Designators" in the bash manpage for those who are interested.

Re:The most useful thing in bash by dllthomas on Wednesday April 17, 2013 @04:35PM (#5567020)
Oh, I didn't mean "You should use $_ instead", just "$_ is also useful."

As I tried to indicate, you should ideally use whichever will make more sense in your history later. If the command you're typing should always be applied to that file, which happened to be the last argument of the previous command this time, then use !$; if the command you're typing should always be applied to the last argument of the previous command, and it just happened to be that file this time, use $_.

Re:The most useful thing in bash by barrkel on Wednesday April 17, 2013 @02:38PM (#5566190)
I find alt-. to be more usable - I know for sure what it will do, as I see the effect of the expansion immediately.
Re:The most useful thing in bash by mturmon on Wednesday April 17, 2013 @02:46PM (#5566242)
A usage pattern with !$ is:

  % mkdir foo
  % chmod g+s !$
  % ls -ld !$   # (make sure it's correct)
  % cd !$
Re:The most useful thing in bash by jdf on Wednesday April 17, 2013 @04:22PM (#5566917)
Alternatively, you can add ':p' to the third line. This will print out the command rather than executing it. Additionally, it's also added to your bash history, so you can add it by pressing the up arrow.

  $ chmod 755 foo
  $ cd !$:p
  cd foo
  $ cd foo
There are some similar "tricks" here:

https://news.ycombinator.com/item?id=5337558

Re:The most useful thing in bash by mturmon on Wednesday April 17, 2013 @06:26PM (#5567742)
I have found "magic-space" more useful than ":p". This auto-expands history substitutions (like !$) when you press the space bar. You can bind it to the space key in tcsh or bash. It also works for !!, !vi, and other substitutions.

So, when you press ! $ <space>, it expands out to "foo" in place, so you can be sure that's what you wanted, and edit it in place.

I think :p predates magic-space. They're both decades old at this point though.

Re:The most useful thing in bash by jacobparker on Wednesday April 17, 2013 @02:46PM (#5566241)
sudo !!

Are you feeling lucky?

Re:The most useful thing in bash by oneandoneis2 on Thursday April 18, 2013 @04:34AM (#5569493)
Agreed - I recently devoted an entire blog post to making the most of !$, it's that useful to me:

http://geekblog.oneandoneis2.org/index.php/2013/04/03/protip...

Re:The most useful thing in bash by banachtarski on Wednesday April 17, 2013 @04:14PM (#5566860)
I would do vi `!!` instead since the backticks are right next to the !'s already.
Re:The most useful thing in bash by wildwood on Wednesday April 17, 2013 @03:33PM (#5566556)
Agreed, one of my favorites. I do this so many times every day:

hg diff -b <filename>

hg commit -m "comments" !$

Re:The most useful thing in bash by gknoy on Thursday April 18, 2013 @03:54AM (#5569391)
Thank you for pointing out that I can do per-file commits. I didn't know that before!

I use 'hg st' so much that I've aliased it as 'st'. It's just about muscle memory now.

Re:The most useful thing in bash by oakenshield on Wednesday April 17, 2013 @05:11PM (#5567284)
alt+. does the same thing as well, and is slightly easier to type.
Re:The most useful thing in bash by martininmelb on Wednesday April 17, 2013 @06:20PM (#5567715)

    $ Make me a sandwich
    What? Make it yourself
    $ sudo !!
    Okay
Re:The most useful thing in bash by spc476 on Wednesday April 17, 2013 @02:33PM (#5566156)
and !N, where N is a number, is the Nth command in your history.
Re:The most useful thing in bash by mturmon on Wednesday April 17, 2013 @02:48PM (#5566257)
And !N:$ is the last argument of the N'th command in history. Even more useful, !vi:$ is the last argument of the last "vi" you did.
Re:The most useful thing in bash by zimpenfish on Wednesday April 17, 2013 @03:28PM (#5566523)
Woah, clearly I need to spend tomorrow re-reading the BASH manual.