历史记录和剪切命令:获取第二个字段

Kim*_*a K 2 linux bash shell cut

我正在尝试从历史命令中获取命令。

ubuntu@ip-172-31-13-192:~/redacted$ history
 1  ls
 2  sudo apt-get install git -y
 3  git clone https://redacted@bitbucket.org/redacted/redacted.git
 4  ls
 5  cd redacted

ubuntu@ip-172-31-13-192:~/redacted$ history | cut -d ' ' -f 2
Run Code Online (Sandbox Code Playgroud)

无输出。怎么了?

Mur*_*nik 5

每行的开头也有空格,因此第 2 列很可能只是另一个空格。由于历史记录的格式是固定的,您可以cut根据字符数来确定,如下所示:

[mureinik@computer /]$ history | cut -c8-
Run Code Online (Sandbox Code Playgroud)