How can I recall the argument of the previous bash command?

前端 未结 7 1984
轮回少年
轮回少年 2020-12-04 04:33

Is there a way in Bash to recall the argument of the previous command?

I usually do vi file.c followed by gcc file.c.

Is there a w

7条回答
  •  遥遥无期
    2020-12-04 04:59

    !!:n where n is the 0-based position of the argument you want.

    For example:

    echo 'one' 'two'
    # "one two"
    
    echo !!:2
    # "two"
    

    The ! prefix is used to access previous commands.

    Other useful commands:

    • !$ - last argument from previous command
    • !^ - first argument (after the program/built-in/script) from previous command
    • !! - previous command (often pronounced "bang bang")
    • !n - command number n from history
    • !pattern - most recent command matching pattern
    • !!:s/find/replace - last command, substitute find with replace

    More info on command history

提交回复
热议问题