-1

This question already has an answer here:

This question is a variant on a previously asked How to repeat currently typed in parameter on bash console?


Many a time, I find myself wanting to slightly rename a file name in the shell, e.g.:

$ mv test_1.py _test_1.py

or

$ mv test_1.py test_1.py.org

I could use the suggestions in How to repeat currently typed in parameter on bash console?, but

is there any bash magic that will allow me to just reference the previously typed parameter?

e.g., if the magic is $M, then - for the above - I'd use:

$ mv test_1.py _$M.py
$ mv test_1.py $M.org

marked as duplicate by Jeff Schaller, don_crissti, G-Man, GAD3R, Eric Renouf Dec 18 '16 at 13:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    You mean any magic not already covered in those answers? I'm not grasping the way that this is a variant of that question (in particular this answer seems like the closest to the "magic" you're looking for) – Eric Renouf Dec 17 '16 at 13:15
  • also this answer, like: mv {,_}test_1.py – Jeff Schaller Dec 17 '16 at 13:36
  • 2
    Read man bash. Search !! – waltinator Dec 17 '16 at 16:19
  • @Eric, thanks for pointing out the !#$ answer, which I somehow missed, and which is what I was looking for. Sometimes it's right under your nose and you miss it. – boardrider Dec 17 '16 at 20:54
1

The magic works in two parts.

First, echo is aliased to e (echo will work also if you want no alias); second, we use "brace expansion":

$ e mv {,_}test_1.py              # Try to see if it works as expected.
mv test_1.py _test_1.py

          # If the arguments are what you want:

$ mv {,_}test_1.py                # Press ↑ (up arrow), remove `e`

$ !*                              # OR: Type `!*` --> last line after arg 0.
$ mv {,_}test_1.py                # If `histverify` is set.

The !* may be expanded with an space if you enable magic-space or if you set shopt -s histverify after you press enter, you will be given a chance to review the effect of the history expansion before pressing enter (again) to execute.

The other example:

$ e mv test_1.py{,.org}
mv test_1.py test_1.py.org        # The result of the brace expansion.
                                  # Review it, and if it is ok:
$ !*                              # type !* (use either space or enter)
                                  # That will depend on how you setup it.

$ mv test_1.py{,.org}        # The command appear again (enter to execute).
$

There is also the history expansion of !# which means the command line typed so far, and selecting the first command :1. If you have magic-space enabled, you type mv test1.py !#:1 and press space, the command will change:

$ mv test_1.py !#:1                # Press space.
$ mv test_1.py test_1.py           # The command line change to this.
$ mv test_1.py test_1.org          # Edit and press enter to execute.

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