0

When I execute printf on the command line, for some reason it includes my prompt text in the output. The command:

printf '%s\n' 'Some text'

The output:

' 'Some text' {~} (myusername@my-PC-name)Some text

Oddly enough, it only does this if I use the newline escape sequence in the format string. If I leave that out:

printf '%s' 'Some text'

I get:

Some text

There must be something wrong with how my prompt has been setup. Below is a snippet from my .bashrc file. Most of this code came with the file. I have removed comments that came with the file and added comments indicating my modifications:

if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
      color_prompt=yes
    else
      color_prompt=
    fi
fi

# I added these lines:    
black="\[\033[0;30m\]"
blue="\[\033[0;34m\]"
green="\[\033[0;32m\]"
cyan="\[\033[1;36m\]"
red="\[\033[0;31m\]"
magenta="\[\033[0;35m\]"
yellow="\[\033[1;33m\]"
gray="\[\033[0;37m\]"
reset_color="\[\033[00m\]"
# end of my additions

if [ "$color_prompt" = yes ]; then
    # I added the following two lines:
    user_host_color="$cyan"
    path_color="$yellow"
    # Below is the original line assigning PS1:
    # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    # Below is my version:
    PS1="${debian_chroot:+($debian_chroot)}${user_host_color}\u@\h${reset_color}:${path_color}\w${reset_color}\\$ "
    # My addition below: unsetting some variables I added
    unset user_host_color path_color
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# My addition below: unsetting variables I added
unset black blue green cyan red magenta yellow gray reset_color

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

This results in a color prompt that looks like: myusername@my-PC-name:/current/path $, where myusername is green, my-PC-name is blue, /current/path is yellow, and $ is green.

Notes about my system: I am running Bash 4.3.48 on Ubuntu 16.04.3 LTS on my Windows 10 (Fall Creators Update) machine via Windows Subsystem for Linux. So this is not the complete Ubuntu desktop system, it's just the command line version available on the Windows Store. Although I'm not sure whether that matters for this issue.

  • 1
    Comment out the prompt coloring and try again. If the questionable behavior disappears then you know what to debug. Even easier, set PS1='$' and built it by hand from there. – AlexP Jan 4 '18 at 2:25
  • Yes, I know the issue is with the initialization of PS1 in .bashrc, I'm just trying to figure out what's wrong with my implementation. The major difference between my version and the original is that my version uses double quotes and includes variables for the color escape sequences. Although, the original version includes the variable $debian_chroot which somehow works in spite of the use of single quotes? I don't fully understand how this works, I'm kind of a noob. I'll try changing my version to use single quotes and see if that helps. – William Beaumont Jan 4 '18 at 3:09
  • I second @AlexP comment. Restore ~/.bashrc to original state and try printf. If it behaves normally then the whole subject of this question changes from printf problem to color escape sequences in Windows Subsystem for Linux problem. – WinEunuuchs2Unix Jan 4 '18 at 4:03

Your Answer

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

Browse other questions tagged or ask your own question.