Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In bash, you can use !* to get all the arguments from the previous command. As an example, if you did cp /some/path /some/other/path and then did mv !*, the second command would be expanded to mv /some/path /some/other/path.

Is there anything like this that can be used to access a specific argument from a command instead of all of them?

share|improve this question

2 Answers 2

up vote 27 down vote accepted

In !*, ! is the history expansion prefix, and * is the word designator that means all arguments. You can memorize the general syntax as bang-line-colon-column (!line:column). There are many possible shortcuts: the default line is the previous line, the default column specifier is “all”, and you can leave off the colon if the column specifier is non-numeric (but !3 would mean line 3). You can use !:0 to refer to the command name, !:1, !:2, etc, to refer to successive arguments, !:$ for the last word, !:* for all arguments, and more.

See also this post by Michael Mrozek at Unix Stack Exchange.

share|improve this answer
1  
I tried it myself and this didn't work, however. After looking at the post by Michale Mrozek, it says to use !:1, !:2. After using the version with the colons, it worked. –  Wuffers Nov 14 '10 at 23:41
    
@MrMan: Oops, right, if you leave off the colon with a number, the number is interpreted as a line number. Sorry about that. –  Gilles Nov 15 '10 at 18:57
    
No problem. So, then I assume that doing !1:1 would point to the first argument of the first line? –  Wuffers Nov 17 '10 at 2:00

Personnally, I really dislike this “expansion with exclamation mark” feature which will even disturb if you try echo "Hello World!" in interactive shells (so sourcing scripts that assume they will be run in non-interactive mode won't work at all).

So, I set set +o histexpand and start recalling arguments with the following method:

Esc, 1, Esc, Ctrl-Y => Insert first argument of previous command.

note that the Esc-trick is because I don't have a meta key.

share|improve this answer
    
I almost never use exclamation points in commands. So the Exclamation point method works perfectly for me. –  Wuffers Nov 17 '10 at 1:58
    
Then enjoy it. Many of the numerous features of your shell are designed for only a few users. –  Benoit Nov 17 '10 at 6:29
    
Actually, you don't have to escape exclamation points. Do echo 'Hello, orld!'. Note the single quotes instead of double ones. –  Wuffers Nov 30 '10 at 23:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.