LotAbout
LotAbout

Reputation: 717

Refer to previous arguments of current command

For example, I would like to do the following:

mv xxxx xxxx.bak

I know I could use this command instead:

mv xxxx{,.bak}

I think this is not direct somehow. It would be wonderful if I could do this:

mv xxxx $1.bak

And sometimes I'd need it like this:

echo xxxx yyyy $1.suffix

I know we can refer to arguments of the previous command using !:n, but can I also refer to arguments of the current command?

BTW, I want to do it directly in the shell, interactively.

Upvotes: 13

Views: 771

Answers (2)

chepner
chepner

Reputation: 531125

The current command line is referenced with !#.

mv xxxx !#:1.bak

I recommend enabling the histverify option if you aren't already using it, so you have a chance to proofread or edit the results of the history expansion before actually executing it. To do so:

shopt -s histverify

Or, if you don't want to enable that option and just want to verify a single command, use the :p modifier to print the expansion instead of executing it:

$ mv xxx !#:1:p.bak
mv xxx xxx.bak
$

Upvotes: 24

Farvardin
Farvardin

Reputation: 5424

one way is to use a variable for this. simply like :

f="file"
cp $f $f.bak

Upvotes: 2

Related Questions