shell 命令 cd !$

myo*_*yol 5 command-line terminal

我看着这个问题并用谷歌搜索,但找不到明确的答案。!$输入终端时会做什么?

我打字

mkdir /path/to/dir
cd !$
Run Code Online (Sandbox Code Playgroud)

这让我/path/to/dir想到了,但我仍然想确切地知道!$运营商一般做什么。

Pan*_*dya 6

从 Bash 手册 > 历史扩展:

  • 事件指示符

    !      Start a history substitution, except when followed by 
           a blank, newline, carriage return, = or  (  (when the 
           extglob shell option is enabled using the shopt builtin).
    !n     Refer to command line n.
    
    Run Code Online (Sandbox Code Playgroud)
  • 单词指示符

    $      The  last word.  This is usually the last argument, but will 
           expand to the zeroth word if there is only one word in the line.
    
    Run Code Online (Sandbox Code Playgroud)

所以,!$只重复上一个命令的最后一个单词

例子:

$ echo Unix and Linux
Unix and Linux
$ echo !$
echo Linux
Linux
Run Code Online (Sandbox Code Playgroud)

$ cat dir1
cat: dir1: Is a directory
$ cd !$
cd dir1
Run Code Online (Sandbox Code Playgroud)

用 执行命令后!$,按 即可?得到结果!$