Chapter 6. Commands for Command Management

History (Check Command History)

History (Check Command History)
Tag:

The history command is used to display commands you run on the shell. This command is useful when you want to check what you have done before or reuse the same command you executed before.

Display a command history

To display a history of commands, run history without any argument. The history command will display up to 1,000 commands.

Command Line - INPUT
history
Command Line - RESPONSE
:
  563  echo $ABC
  564  sudo chmod u+x variable_parent.sh
  565  sudo chmod u+x variable_child.sh
  566  ./variable_parent.ssh
  567  echo $ABC
  568  source variable_parent.sh
  569  echo $ABC
  570  env
  571  env | grep ABC=
  572  history

Display a limited number of commands

If you want to display only the past four commands, run history 4.

Command Line - INPUT
history 4
Command Line - RESPONSE
  570  env
  571  env | grep ABC=
  572  history
  573  history 4

Use grep to display certain commands

If you want to show only commands with certain strings, use the grep command. For example, if you want to display sudo commands only, run history | grep sudo.

Command Line - INPUT
history | grep sudo
Command Line - RESPONSE
:
  207  sudo chmod u+x test_child.sh
  219  sudo chmod u+x variable_parent.sh
  220  sudo chmod u+x variable_child.sh
:

The history command is used to display commands you run on the shell. This command is useful when you want to check what you have done before or reuse the same command you executed before.

Display a command history

To display a history of commands, run history without any argument. The history command will display up to 1,000 commands.

Command Line - INPUT
history
Command Line - RESPONSE
:
  563  echo $ABC
  564  sudo chmod u+x variable_parent.sh
  565  sudo chmod u+x variable_child.sh
  566  ./variable_parent.ssh
  567  echo $ABC
  568  source variable_parent.sh
  569  echo $ABC
  570  env
  571  env | grep ABC=
  572  history

Display a limited number of commands

If you want to display only the past four commands, run history 4.

Command Line - INPUT
history 4
Command Line - RESPONSE
  570  env
  571  env | grep ABC=
  572  history
  573  history 4

Use grep to display certain commands

If you want to show only commands with certain strings, use the grep command. For example, if you want to display sudo commands only, run history | grep sudo.

Command Line - INPUT
history | grep sudo
Command Line - RESPONSE
:
  207  sudo chmod u+x test_child.sh
  219  sudo chmod u+x variable_parent.sh
  220  sudo chmod u+x variable_child.sh
:
Tag: