12

How can I configure my bash prompt to have a newline before it?

Instead of:

Output1
Output2
kshitiz@ksh$

It should have:

Output1
Output2

kshitiz@ksh$
0

4 Answers 4

13
cd $HOME
more >> .bashrc << 'EOT'
PS1='\n$USER:$PWD>' ; export PS1
EOT

This will do it permanently for all your future terminal and console sessions.

To refresh your current sessions with this setting :

. ~/.bashrc
6

Find where ever the prompt is defined on your system, typically I grep for PS1 in /etc/bashrc, /etc/profile.d/* or $HOME/.bash*.

Then add a \n to the beginning of that definition.

So for example on my Fedora 19 system:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

So I'd change this line to this:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\n[\u@\h \W]\\$ "

Alternatively if you don't know where it's being defined you can still change it as you'd like using this trick. In your $HOME/.bashrc file simply add this line to the bottom of the file:

export PS1="\n$PS1"

Example

$ export PS1="\n$PS1"

$ ls
ve2_sq021_sc001_v09.0101.jpg  ve2_sq021_sc001_v09.0103.jpg
ve2_sq021_sc001_v09.0102.jpg  ve2_sq021_sc001_v09.0104.jpg

$ ls
ve2_sq021_sc001_v09.0101.jpg  ve2_sq021_sc001_v09.0103.jpg
ve2_sq021_sc001_v09.0102.jpg  ve2_sq021_sc001_v09.0104.jpg

$ 
4

I was looking for an approach to insert a new line only when last command output didn't have a new line at the end of its output. See below how I managed to do so:

# https://github.com/dylanaraps/pure-bash-bible#get-the-current-cursor-position
new_line_ps1() {
  local _ y x _
  local LIGHT_YELLOW="\001\033[1;93m\002"
  local     RESET="\001\e[0m\002"

  IFS='[;' read -p $'\e[6n' -d R -rs _ y x _
  if [[ "$x" != 1 ]]; then
    printf "\n${LIGHT_YELLOW}^^ no newline at end of output ^^\n${RESET}"
  fi
}

PS1="\$(new_line_ps1)$PS1"

Credits to

1
  • this is the true answer I was looking for.
    – jpbochi
    Jul 13, 2022 at 19:38
1

Use

PROMPT_COMMAND="echo"

Using

PS1="\n$PS1"

messes up bash's character count if you are using colored prompt, which may lead to incorrect wrapping of terminal commands.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .