Zum Inhalt

bash cheatsheet


Last run commands

Traverse the history in ~/.bash_history


or if you forget sudo, reference the last command using !!:

sudo !!

Search last commands

Ctrl+R will toggle inverse search in your history.

Arguments of last successfull command

We ran:

cat log-edabadfdads-xasdsf.text 
but the log is too big. So we want to tail it. You can write tail and hit Alt+. to get the last argument.


Exit a program

Ctrl+C will normally exit any shell program.

Exit Codes

Exit code of last run command

echo $? will show the exit code of the last run command.


Paste from clipboard

Ctrl+Shift+V pastes clipboard content to the terminal.

Shell tricks

  • Ctrl+K cuts characters right to the cursor
  • Ctrl+U cuts characters left to the cursor
  • Ctrl+Y bring back last cut characters

Open buffer

When starting a line like a loop:

for item in "1 2 3 4"; do

you can hit Ctrl+X+E to open a buffer in your editor (echo $EDITOR). Complete the code with

for item in "1 2 3 4"; do
   echo $item
Quit the editor with saving and the content will run.

Go to the home directory

Change to the home directory of $USER.

cd ~
cd $HOME

Go back to last directory

cd - will go back to the last directory

Inspecting files

End of file

Display the end of a logfile following new incoming data:

less +F /var/log/syslog
  • Hit Ctrl+C to detach from the end of the file to scroll up.
  • Hit Shift+F to glue to the end.

Display last 100 lines of a file with tail:

tail -n 100 /var/log/syslog


Reset the terminal when behaving wierd

reset wil reset the terminal, which can be used instead of closing the terminal.


Letztes Update: March 25, 2023
Erstellt: December 15, 2022