Which Bash shortcut or command re executes a specific command in the history list in Linux?

The bash history can do many helpful things, and the search with Strg-r that Terry Wang mentioned is an important one among them. But it is also possible to do exactly what you asked for.

You can re-do the previous command with !!. With this, you can also edit the previous command. If for example you forgot to get root privileges for a command

apt-get install a-long-list-of-packages

you don't have to retype all of that again. Instead just call

sudo !!

If you want to re-execute the command at a specific position from your history, you can also use !, for example

!3

to re-execute the command at position 3. Be aware that this counts from the top. So if you're storing 500 commands in your history, !1 would be "500 commands ago". You can also use negative numbers. For example

!-2

would re-execute the second last command.

You can also re-execute the last command that started with a string like

!apt-

which would re-do the last line that started with "apt-". If you want the last command where the string appeared anywhere in the line, you can use something like

!?pt-ge

There are more interesting things the bash history can do. Just to give an impression of the wide range of possibilities, you can specifically access a parameter of a command from history. So

!-5:3:p

would print the third parameter to the fifth from last command.

EDIT: Regarding Rudie's comment below, with the standard settings this bash history expansions are indeed executed directly. It's probably best described like this: A call like !-3 is replaced by the shell with the third last command from your history and then your input (with the replacement) executed. So if you type !-3 and press ENTER and your third last command was ls ~, it's in effect the same as if you typed ls ~ again and pressed ENTER "on your own".

If you don't want that, you can set the shell option histverify. For setting and unsetting shell options, you might want to read up on the shopt command. With histverify set, a call like !-3 only writes the replacement from your history to your command line, but doesn't execute it directly. You have, so to speek, press the crucial ENTER yourself - or refrain from it, if you choose to.

Which Bash shortcut or command re executes a specific command in the history list in Linux?

Bash is the default command-line shell on most Linux distributions, from Ubuntu and Debian to Red Hat and Fedora. Bash is also the default shell included with macOS, and you can install a Linux-based bash environment on Windows 10.

The bash shell features a wide variety of keyboard shortcuts you can use. These will work in bash on any operating system. Some of them may not work if you’re accessing bash remotely through an SSH or telnet session, depending on how you have your keys mapped.

RELATED: 10 Basic Linux Commands for Beginners

Working With Processes

Use the following shortcuts to manage running processes.

  • Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal. This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.
  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.
  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

Which Bash shortcut or command re executes a specific command in the history list in Linux?

RELATED: How Linux Signals Work: SIGINT, SIGTERM, and SIGKILL

Controlling the Screen

The following shortcuts allow you to control what appears on the screen.

  • Ctrl+L: Clear the screen. This is similar to running the “clear” command.
  • Ctrl+S: Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
  • Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.

Moving the Cursor

Use the following shortcuts to quickly move the cursor around the current line while typing a command.

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
  • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
  • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.

Deleting Text

Use the following shortcuts to quickly delete characters:

  • Ctrl+D or Delete: Delete the character under the cursor.
  • Alt+D: Delete all characters after the cursor on the current line.
  • Ctrl+H or Backspace: Delete the character before the cursor.

Fixing Typos

These shortcuts allow you to fix typos and undo your key presses.

  • Alt+T: Swap the current word with the previous word.
  • Ctrl+T: Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
  • Ctrl+_: Undo your last key press. You can repeat this to undo multiple times.

Cutting and Pasting

Bash includes some basic cut-and-paste features.

  • Ctrl+W: Cut the word before the cursor, adding it to the clipboard.
  • Ctrl+K: Cut the part of the line after the cursor, adding it to the clipboard.
  • Ctrl+U: Cut the part of the line before the cursor, adding it to the clipboard.
  • Ctrl+Y: Paste the last thing you cut from the clipboard. The y here stands for “yank”.

Capitalizing Characters

The bash shell can quickly convert characters to upper or lower case:

  • Alt+U: Capitalize every character from the cursor to the end of the current word, converting the characters to upper case.
  • Alt+L: Uncapitalize every character from the cursor to the end of the current word, converting the characters to lower case.
  • Alt+C: Capitalize the character under the cursor. Your cursor will move to the end of the current word.

Tab Completion

RELATED: Use Tab Completion to Type Commands Faster on Any Operating System

Tab completion is a very useful bash feature. While typing a file, directory, or command name, press Tab and bash will automatically complete what you’re typing, if possible. If not, bash will show you various possible matches and you can continue typing and pressing Tab to finish typing.

  • Tab: Automatically complete the file, directory, or command you’re typing.

For example, if you have a file named really_long_file_name in /home/chris/ and it’s the only file name starting with “r” in that directory, you can type /home/chris/r, press Tab, and bash will automatically fill in /home/chris/really_long_file_name for you. If you have multiple files or directories starting with “r”, bash will inform you of your possibilities. You can start typing one of them and press “Tab” to continue.

Which Bash shortcut or command re executes a specific command in the history list in Linux?

Working With Your Command History

RELATED: How to Use Your Bash History in the Linux or macOS Terminal

You can quickly scroll through your recent commands, which are stored in your user account’s bash history file:

  • Ctrl+P or Up Arrow: Go to the previous command in the command history. Press the shortcut multiple times to walk back through the history.
  • Ctrl+N or Down Arrow: Go to the next command in the command history. Press the shortcut multiple times to walk forward through the history.
  • Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it.

Bash also has a special “recall” mode you can use to search for commands you’ve previously run:

  • Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.
  • Ctrl+O: Run a command you found with Ctrl+R.
  • Ctrl+G: Leave history searching mode without running a command.

Which Bash shortcut or command re executes a specific command in the history list in Linux?

emacs vs. vi Keyboard Shortcuts

The above instructions assume you’re using the default keyboard shortcut configuration in bash. By default, bash uses emacs-style keys. If you’re more used to the vi text editor, you can switch to vi-style keyboard shortcuts.

The following command will put bash into vi mode:

set -o vi

The following command will put bash back into the default emacs mode:

set -o emacs

Which Bash shortcut or command re executes a specific command in the history list in Linux?

With a few of these in your toolbelt, you’ll be a Terminal master in no time.

READ NEXT

  • › How to Be More Productive in Ubuntu Using Keyboard Shortcuts
  • › How to Use the Mac Terminal’s Hidden Task Manager to See Background Processes
  • › How to Type Less and Work Faster in the Linux Terminal
  • › Keyboard Ninja: 21 Keyboard Shortcut Articles
  • › How to Use Your Bash History in the Linux or macOS Terminal
  • › The Steam Winter Sale Is Here
  • › Get UPDF, a Powerful PDF Editor with OCR for 56% Off
  • › ASUS Breaks World Record With 9 GHz Overclocked Intel CPU

How-To Geek is where you turn when you want experts to explain technology. Since we launched in 2006, our articles have been read more than 1 billion times. Want to know more?

Which bash shortcut or command is used re

Pressing Esc+. Which Bash shortcut or command is used to re-execute a recent command by matching the command name? e. Pressing Esc+.

Which bash shortcut or command is used to re

1) Ctrl + P This is the most dependable shortcut in Linux to execute the last run command in the terminal.

Which command is used to execute the previous command in Linux?

Using CTRL + P and CTRL + R You can use a combination of 'CTRL + P' and 'CTRL + R' to output the last command that you used on the terminal. And then, if you press the <Enter> button after this combination, you will also be able to run that command as well.

How to list history of commands in Linux?

Using a Reverse Search of Linux Command History To enter this mode you simply press ctrl and r. You can then enter a search term and use repeat presses of ctrl and r to step back through the list of previous commands containing that term.