Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Bash how to remove empty line after command executed in terminal

Tags:

git-bash

I update the Windows version Git Bash to 3.0.2, now I faced a very annoying problem, it is hard to be explained. In the terminal, after each command is executed, it will always automatically add a new empty line, for example:

User@path $ CMD-1
                         <<-- new line
User@path $ CMD-2
                         <<-- new line
User@path $ CMD-3
                         <<-- new line
User@path $

It is showing like the picture: enter image description here

My question is how can I remove this type of command-tail new line?

And even further, which source file is adding those tail-new-lines after every command executed? Cause I have almost checked every file that related to PS1 and inputs, but I still can't figure out why this stupid feature happens. Many thanks for your help.

like image 260
Xiang Avatar asked May 16 '26 13:05

Xiang


1 Answers

This is caused by how your PS1 shell variable is configured.

If you run the following command in Git Bash it will print the current value of your $PS1 variable:

echo "$PS1"

For example, the default value of $PS1 on my computer is set to:

\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$
                                ^^                                                                                       ^^

Note: Yours will appear differently to what is shown above because I'm using an earlier version of Git Bash. However, what is important to note is the two newline characters \n (I've added caret symbols ^^ to clearly indicate them). The value of your $PS1 will probably have just one newline character \n.

Essentially it's these/this newline (\n) character(s) that produce the blank newline after running a command.


Solution:

To remove the "command-tail new line" consider running the following compound command:

echo $'\n'"export PS1='${PS1//\\n/}'" >> ~/.bash_profile

Explanation:

  • This essentially adds new code to either;

    1. The end of your existing .bash_profile startup file (if the file does already exists in your HOME directory).

    2. Or at the beginning of a newly created .bash_profile file in your home directory (if the file does not already exist).

    For instance, your .bash_profile file will now include a line that reads something like this:

    export PS1='<newvalue>'

  • The line that is is added to .bash_profile utilizes export to define a new value for the $PS1 shell variable. The <newvalue> part will actually be whatever your current PS1 value is minus any newline \n characters.

  • The part that reads ${PS1//\\n/} in the aforementioned compound command utilizes parameter expansion to remove all newline characters from your current $PS1 value.

After running the aforementioned compound command the "command-tail new line" issue should be resolved the next time you create a new session, (i.e. create a new window, or relaunch Git Bash).


Edit: Changing PS1 value in the git-prompt.sh file:

If you don't want to override the default PS1 value using the aforementioned solution, you can change the value(s) in the git-prompt.sh file that Git Bash creates during installation.

To do this:

  1. Open the file named git-prompt.sh in your code editor. The file is typically located at:

    C:/Program Files/Git/etc/profile.d/git-prompt.sh

  2. Find instances of newline characters, i.e. \n and delete them.

    For instance; my git-prompt.sh file contained a couple of lines that read:

    PS1="$PS1"'\n'
    

    I simple deleted the '\n' part so that they read:

    PS1="$PS1"
    
  3. Save the changes that you made to the git-prompt.sh file, then create a new session, (i.e. create a new window, or relaunch Git Bash).

like image 128
RobC Avatar answered May 19 '26 04:05

RobC