8

When using the command line, often it gets very cluttered. Making it inconvenient to examine past commands and their outputs for example.

I would like to have a newline added each time before the command prompt is shown. Like so:

<clutter>
<blank line>
name@machine:~$

I use the bash shell. How can this be achieved?

21

One way to achieve this is by modifying the .bashrc file. Simply place the following at the end of the .bashrc file.

PS1="\n$PS1"

To explain how this works, PS1 is the variable containing what should be displayed as the prompt. All this is saying is "set PS1 to the previous contents of PS1, with a newline character prepended". Putting it in .bashrc on most distros just makes bash run it every time you open an interactive shell (but not a login shell - see Difference between Login Shell and Non-Login Shell?).

  • 2
    To explain how this works, PS1 is the variable containing what should be displayed as the prompt. All this is saying is "set PS1 to the previous contents of PS1, with a newline character prepended". – Muzer Aug 11 '15 at 15:20
  • @Muzer Maybe your comment could be an edit – rpax Aug 11 '15 at 20:21
  • @rpax good idea, submitted. Added a bit about .bashrc itself, too. – Muzer Aug 12 '15 at 8:37
  • Satisfyingly, executing PS1="\n$PS1" in a terminal has instant effect :) – mwfearnley Nov 18 '15 at 16:22
  • Works totally fine, other than after entering clear (or pressing Ctrl+l) the first line shown is an empty line. For some it might be a small detail, but I'd much prefer the first line to be the prompt. – Robert Kusznier Aug 13 '19 at 1:54
6

You can use PROMPT_COMMAND:

PROMPT_COMMAND="printf '\n';$PROMPT_COMMAND"

or:

PROMPT_COMMAND="echo;$PROMPT_COMMAND"
  • 2
    Should the first one be PROMPT_COMMAND="printf '\n';$PROMPT_COMMAND"? In its present form it dint seem to do anything when I placed it in the .bashrc file. – Slothworks Aug 11 '15 at 7:19
  • @Slothworks: Yes, of course, my mis-typing, fixed it. – cuonglm Aug 11 '15 at 7:26
  • More than one way to do it indeed :) Thank you for your answer. – Slothworks Aug 11 '15 at 7:35
  • 2
    Slothworks's alternative is better, because yours executes an extra command after every command that is run at the command line. printf and echo are very lightweight, of course, but it still doesn't make sense to use PROMPT_COMMAND when PS1 will do the job. – Mikkel Aug 11 '15 at 18:00
  • Note, if you wanted to put something in the command that changed every time you ran the prompt, then you want PROMPT_COMMAND and not PS1. This is commonly used with git to display which branch of the repo is active. Google for PROMPT_COMMAND suggestions to see what can be done. – Walter Aug 11 '15 at 21:04
3

Alternative: leave a line in the PS1= prompt of your .bashrc. Here's literally how I set up .bashrc on every linux machine I have:

PS1=' 
serg@ubuntu [$(pwd)]
================================
$ '

As you see in my example above, username is hardcoded into the prompt. You can of course use escape sequences that bash or another shell (e.g. ksh) provides, but a little more neutral option would be to do use parameter substitution with commands like whoami (report your username) and hostname(obviously, reports hostname). For instance:

PS1='
$(whoami)@$(hostname):$(pwd)
$ '

For more fun stuff with the prompt, .bashrc, and parameter expansions, check out my answers here:

How to check battery status using terminal?

How can I get my terminals / shells to have custom designs in it? and

How to show a running clock in terminal before the command prompt?

  • Why the user name and machine name are hard coded? Would be nice if they were parametrized – rpax Aug 11 '15 at 20:23
  • @rpax my actual username is different. That's my name that you see in the prompt. Machine name is also different. – Sergiy Kolodyazhnyy Aug 11 '15 at 20:28
  • I meant some kind of $(whoami) instead of usernamehardcoded – rpax Aug 11 '15 at 20:30
  • @rpax oh, that can be done as well. I'll include that into my answer – Sergiy Kolodyazhnyy Aug 11 '15 at 20:34
  • 4
    Note you can use \u and \h for your username and hostname, respectively. You can see more special characters (for e.g. the current time in various formats) here – Yossarian Aug 12 '15 at 8:21

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.