Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: refer to previous arguments of current command

Tags:

bash

For example, I would like to do the following:

mv xxxx xxxx.bak

I know I could use the following instead:

mv xxxx{,.bak}

which I think is not direct somehow. It would be wonderful if I can do this like:

mv xxxx $1.bak

And sometimes we'll need it like this:

echo xxxx yyyy $1.suffix

I know we can refer to arguments of the previous command using "!:n", can I instead refer to arguments of the current command likewise?

BTW, I want to do it directly in shell(interactively). Thanks.

like image 435
LotAbout Avatar asked Apr 23 '14 12:04

LotAbout


2 Answers

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
$
like image 134
chepner Avatar answered Sep 28 '22 01:09

chepner


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

f="file"
cp $f $f.bak
like image 45
Taher Khorshidi Avatar answered Sep 28 '22 03:09

Taher Khorshidi