In bash, can you alias a null command to something?  In other words, recognize that the user has pressed just the Enter key at the shell prompt?  For example, if you want to clear the screen with a blank shell entry, how do you alias the Enter to the clear command?

  • 1
    Alias name must have a least one letter. Try alias c='clear' – DavidPostill Mar 21 '19 at 21:56

Here’s how to (effectively) alias an empty command to something:

Put the following into your .bashrc:

    if [ "$BASH_COMMAND" != command2 ]
    return 0
trap command1 debug
    if [ ! "$command_flag" ]
        echo "clear"                            # Put whatever command(s) you want here.

If necessary, replace command1 and command2 consistently with words that you’re less likely to type randomly.

  • trap command1 debug will cause the command1 function to be executed every time you (the user) type a command.
    • If the most recent command to be executed was not command2, command1 will set command_flag.
  • Assigning command2 to PROMPT_COMMAND will cause command2 to be executed whenever the shell is about to issue a prompt.
    • If command_flag has a non-blank value, that means command1 was just executed, which means the user just ran a command.  In this case, do nothing.
    • If command_flag does not have a non-blank value, that means the user just hit Enter.  In this case, do what you want to do (e.g., clear the screen).
    • In either case, set command_flag to blank, so everything will work on the next cycle.

Note: I’m pretty sure that this is bash-specific (i.e., not supported by POSIX).

This answer was basically copied from my answer to Adding vertical space after command in bash.  See also tell if last command was empty in PROMPT_COMMAND for other approaches.


TL;DR: Ctrl-l will clear the screen without printing a character or pressing enter. This is probably the closest you'll get to your desired functionality. You can make a command run before or after every command, or you can make an alias for a single character. You cannot alias the empty string.

Aliases are not necessarily designed to be the whole thing entered on a line.

alias ll='ls -l'

is a good example of this. You can do ll /path/to/other/dir and get results.

All an alias does is literally change the first word(s) typed on the command line to the value to you gave it

The only requirement for an alias to work is that it must be followed a non-printing character (space/tab/newline). In our case, ll by itself is valid, but ll-/mypath/ isn't, because it is possible (and frequently done) to alias a few characters that can be the start of another command (i.e., alias gi='git init' can only resolve exactly gi, not git, which is what most people want and expect).

It isn't possible to alias something to the empty string, since that would be at the start of every command line entry.

If you want a command to run every time, you can use PS0 (assuming a recent-enough version of bash) to run something before every command or PROMPT_COMMAND to run something after every command. For example:


would echo the return value of the command you ran after every command you run.

Bash does not have a good way of completing an action if and only if you've entered an empty line on the command line. Bash is not whitespace sensitive in this way.

  • Thanks @jeremysprofile that was quite informative. So there is no way to test the last bash command run in PROMPT_COMMAND for example? – Ilias Karim Mar 22 '19 at 14:35
  • You can test the last command run. If you're really looking for some esoteric stuff, @Scott's answer does exactly what you asked (I didn't know that was possible). – jeremysprofile Mar 22 '19 at 15:16
  • My only real grievance with command-l is it doesn’t print a prompt. – Ilias Karim Mar 22 '19 at 15:44
  • When I do ctrl-l, the only thing at the top of my terminal is my prompt. What do you see? – jeremysprofile Mar 22 '19 at 16:04
  • This can be handled by bash; specifically, by bash’s “readline” function.  If I type bind -P | grep clear, I get clear-screen can be found on "\C-l"., which says that Ctrl+L (C-l) is bound to the “clear-screen” function.  What do you get when you issue that bind | grep command?  If you don’t have the Ctrl+L key bound, it’s possible that your terminal is handling the keystroke without telling bash (so bash doesn’t know to re-issue the prompt). – Scott Mar 22 '19 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.