I‘m a new Ubuntu Linux user. In Linux, Apple OS X or Unix-like systems, how do I reissue or repeat a long command without retying it?

You need to use the history command to display or manipulate the history list on a Linux or Unix-like systems. This command displays the list of commands previously typed with line numbers, prefixing each modified entry with a *. [donotprint]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements bash/csh/zsh
Est. reading time 1m
[/donotprint] The bash shell supports a history expansion feature that is similar to the history expansion in csh.

Display list of previously typed commands

Simply type the following command:

history
history 10
history | less
history | grep 'command-name-here'

Sample outputs:

Fig.01: Bash history command output

How do I reissue a long command without retyping it?

To reissue a command in bash/csh/tcsh/zsh shell, type ! the exclamation point followed by the number of the command you would like to run or repeat. For example, if you would like to reissue command ‘ssh root@v.b2’ from the above output i.e. command # 80, type:

!80

Scrolling through the command line history

You can also scroll through the command line history simply by using the [up] and [down] arrow keys too.

Searching the command line history

Press [CTRL-r] from the shell prompt to search backwards through history buffer or file for a command. After pressing [CTRL-r] just type first few command letter such as ssh:

(reverse-i-search)`ssh ': ssh -X vivek@nas01

To search all ssh related commands press [CTRL-r] again:

Gif.01: Bash/tcsh/zsh: Command search demo

To repeat last command just type !! at a shell prompt

Say you type a long command:

ssh -X -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /Users/veryv/.ssh/google_compute_engine -A -p 22 veryv@173.255.113.19 --

To repeat the same last command again, just type !!:

!!

Or you can also refer to the previous command using:

!-1

To repeat to the most recent command starting with word ‘ssh’ type:

!ssh

For more info see man pages – tcsh(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Irfan Feb 2, 2015 @ 22:28

    Many thanks for sharing the information.

  • Jeremy Boden Feb 3, 2015 @ 2:25

    Note that previously used commands can be accessed in command entry or terminal windows across reboots.

  • Jules Feb 3, 2015 @ 14:59

    Sometimes is is dangerous to just use the exclamation point with the first letter. It may pull up and execute a command you did not intend. To avoid this use the print option (‘:p’) after. For example to just print out the last command beginning with r, but not execute it you would enter the following:

        !r:p

    Bash will print out the command and make it the final command in your history, for example:

       rm *xml

    Now you can press the up arrow and execute that command or edit it and then execute it by pressing the Enter key.

  • Chris F.A. Johnson Feb 5, 2015 @ 21:28

    It is dangerous to use a printable character for anything other than self-insert.

  • DePe Feb 28, 2015 @ 15:22

    Make a text file of the script, and drag and drop it to the terminal, and run it. No Idea how to do it though

  • Wesley Steinbrink Jul 16, 2015 @ 17:24

    I really like Ctrl-r. It is useful for accessing a command by part of the command in the middle also. You can keep adding letters to your search or doing Ctrl-r to cycle through what matches that that you already have input. When you are satisfied that it is a command you want to run – then hit enter. If it is almost the command that you want to run, but requires some more modification, then hit .
    Ctrl-r can also be used on an existing line to go back to a character in that line. To go back to the closest space – Ctrl-r and
    For more control within an existing line, use Ctrl-Alt-] to move backwards
    For more control within an existing line, use Ctrl-] to move forwards
    For these can be replaced by any character.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum