13

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.

1
  • See also this answer that uses !$ to refer to the current command and uses $ to refer to the previous (instead of the very first) argument. For example, ocrmypdf --deskew --optimize 3 my.pdf !#$:r_ocr.pdf will expand the last argument to my_ocr.pdf. Commented Sep 24, 2022 at 19:09

2 Answers 2

24

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
$
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.