Search code examples
bashshellcommand

How to swap parameters in shell command


I have a command with parameters in a shell:

Command -a -b

Assume a and b are very long directory names, and they are not always the first and last parameters.

However, I need to use this command again. I use Ctrl+p to go back to the previous command, but this time I need to reverse the order of a and b:

Command -b -a

Instead of retyping the parameters again, is there any way to swap them easily?


Solution

  • Yes you can use history substitution:

    $ echo foo bar
    foo bar
    $ !:0 !:2 !:1          # previous command with second arg then first arg
    echo bar foo
    bar foo
    $