Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW if you use Bash shell you can also use history expansion to shorten the mkdir && cd line further (the line already includes tilde expansion):

  mkdir ~/www/example.com && cd !$
!$ is the last argument of the preceding command


You mean "$_". !$ refers to the previous command in the history.


!! refers to the previous command in the history. !$ refers to the last cmd-line option in the history.


I know that, the bit I want to emphasize is that it won't be the mkdir argument that is on the same line.



Seriously, try it. History expansion works on input lines separated by the user asking the shell to run them, it can't cross reference command arguments on the same command line.


You are correct, so have an upvote.

&& in OS X seems to put the left side into history and works as I described, but it didn't work when I tried it in Ubuntu 10. $_ works in both.


I have tried both, the difference is only that !$ echos the expanded command, while $_ simply runs the expanded command; both create a directory and cd into it.

(I'm running OS X, FWIW)


    mkcd() { mkdir "$1" && cd "$1" }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: