3

I saw a blog post awhile back about a unix time-saving trick that for the life of me I have not been able to remember and the man page does not list.

Basically it was a way to cd into the directory you just copied or moved something to. Example:

$pwd
/home/stedy
$mv temp /projects/trunk
$cd something
$pwd
/projects/trunk

Has anyone ever seen this before? What am I forgetting after the cd command?

3 Answers 3

3

One option is to use history commands:

$pwd
/home/stedy
$mv temp /projects/trunk
$cd !:2
$pwd
/projects/trunk

!:2 takes the second argument of the previous command. You can vary pleasures immensely with modifiers, your best friend is man bash or info bash and look for the history expansion (and modifiers).

1
  • 1
    !$ gives the last argument of the previous command and is more likely to consistently do what is intended without having to think about how many arguments there were, particularly in the case of something like cp foo bar baz dest/ Dec 7, 2010 at 2:06
6

You can press Alt-. to get the last argument of the previous command in Bash, ksh and zsh. The keystroke is actually designated as Meta-. which usually means pressing Esc then ., but the Alt combination is commonly available. Note that an underscore will work in place of the dot. See the Readline section of man bash for more information. The readline function is called yank-last-arg.

2

In most shells the last parameter you gave on the previous command is in $_

mkdir /some/dir
cd $_
# now in /some/dir

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .