74

I wish to add a new line on every prompt. Instead of the cursor showing on the line as name@machine:~$ _,

I want it to be on the next line, like as:

name@machine:~$
_

Thanks.

3 Answers 3

96
  • Open the file ~/.bashrc (or /etc/bash.bashrc if it should work globally for all users).

  • Locate the variable called PS1.

  • Simply put an \n at the end of the value of the PS1 variable.

3
  • 24
    The easiest way to do this is to add PS1="$PS1\n" to the bottom of your ~/.bashrc file. Dec 6, 2010 at 14:09
  • 3
    Guess that's easier to maintain - why don't make it a new answer? :)
    – htorque
    Dec 6, 2010 at 14:44
  • yeah, I did that. there were too many lines to append a \n to, So i created a new variable. Dec 6, 2010 at 17:04
21

I prefer using a custom .bashrc file

First, append the following lines to your ~/.bashrc file:

##
## INCLUDE CUSTOM `.bashrc` CODE
##
if [ -f ~/.bashrc_custom ]; then
    . ~/.bashrc_custom
fi

Create the custom file:

touch ~/.bashrc_custom`

Finally open it and put the following lines into:

# File: $HOME/.bashrc_custom
# THIS FILE IS A USER-CUSTOM BASHRC FILE TO KEEP CLEAN THE DEFAULT ~/.barshrc FILE.
# PUT THERE ANY CUSTOM CODE MANUALLY ADDED BY YOU


# Add a new line at the end of the command prompt
#PS1=${PS1}\\n
PS1=${PS1%?}
PS1=${PS1%?}\n'$ '

The next opened shell session will looks like following:

user@host:~
$ <your-next-command-will-be-rendered-here>
1
  • 5
    Why do you recommend using custom .bashrc file? Isn't exactly why .bashrc in your home directory exists? I understand that you can use and modify default generated .bashrc, but than when you want to use .bashrc on other OSes you can't because you depend on default .bashrc file and if it's not the same it can behave differently. Nov 24, 2017 at 16:19
6

This was painful, but in the end, due to a complicated custom PS1 setup with custom colors, this is the only thing that worked for me:

new_line() {
    printf "\n$ "
}
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]$(new_line)'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)$(new_line)'
fi
1
  • 1
    Great solution. I kindly suggest other new viewers for this solution to view the current PS value by using echo $PS1. It will have a lot of hints and we can work on top of it especially about the color if you already configured one so.
    – haxpor
    Apr 23 at 10:46

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.