Is there a way to save a command in Ubuntu terminal if I haven't entered it?

This is not your terminal, this is your shell.

The name for the shell mechanism that you are looking for is a kill buffer. People forget that shell command line editors have these. ZLE in the Z shell has them, as have GNU Readline in the Bourne Again shell, libedit in the (FreeBSD) Almquist shell, the Korn shell's line editor, and the TENEX C shell's line editor.

In all of these shells in emacs mode, simply go to the end of the line to be saved, kill it to the head kill buffer with ⎈ Control+U, type and run the intermediate command, and then yank the kill buffer contents with ⎈ Control+Y. Ensure that you do not do anything with the kill buffer when entering the intermediate command.

In the Z shell in vi mode, you have the vi prefix sequences for specifying a named vi-style buffer to kill the line into. You can use one of the other buffers instead of the default buffer. Simply use something like " a d d (in vicmd mode) to delete the whole line into buffer "a", type and run the intermediate command, and then put that buffer's contents with " a p.

In their vi modes, the Korn shell, GNU Readline in the Bourne Again shell, and libedit in the (FreeBSD) Almquist shell do not have named vi-style buffers, only the one cut buffer. d d to delete the line into that buffer, followed by putting the buffer contents with p, will work. But it uses the same vi-style buffer that killing and yanking will while entering the intermediate command.


You don't want to prepend echo (or any other no-op-type command) to your line, since you may have I/O redirection, and they would still be executed, potentially overwriting any file you might need to still access (until you are ready to execute your command).

Instead, go to the beginning of the line and enter the comment character, #. Then you can press Enter, and the line will be saved in your history.

If you are in vi mode, ksh and bash (at least) have a specific command for this: enter command mode (press Esc) then press the # character.

This is specifically for this use-case. The description of this command in the O'Reilly Learning the Korn Shell book says:

Prepend # (comment character) to the line and send it to the history file; useful for saving a command to be executed later without having to retype it. If the line already starts with a #, remove the leading # and any other comment characters that follow newlines in a multiline command.


If you are running zsh, you can use Ctrl-Q, which runs the command push-line. It will save what you have currently typed onto a stack, clear the prompt, and then pop the stack after you enter your next command. I use this all the time for exactly the situation you describe.