BASH, $PS1 and some other rambling. (BASH an application? There used to be a scripting sub-forum?)

Hi folks. Every couple of years or so I forget that I am too stupid to be allowed near a computer, and attempt some simple bash work. This phase lasts until I have permanently broken my installation(s) or I have been committed to a secure home for the bewildered. Or both. This is a purely temporary state, and is usually sparked by a desire to follow everyone’s advice and do some backing up.
So I once more try to learn all the switches and options for cp. Then I get the feeling that I should be using rsync. But I need to tidy up before backing up, so I try to digest man pages for diff and the usual stumbling block, find.
I build a number of test directories, of similar structure to the ones I need to back-up. And start to apply my new-found ‘knowledge’. Nothing works as I expect it to, and even when it does I have NO CONFIDENCE whatever that my carefully constructed BASH scripts or functions (never more than two commands together!) will work in real life, and not trash my ‘precious’ data.

Whilst in the process of trying to improve my bash skillz, I have been looking in to changing my default prompt(s) mainly so that they stand out in long scroll-backs of output. A simple task?? Why yes, and hundreds of articles, blogs and posts on the subject, all over the internet.
But, because it is me, and my computer likes to remind me that I should not be bothering my pretty head with such stuff, it throws some obstacles in my path, just for fun.

Before changing anything, like a good boy, I want to know what is there already and how to get back there if I want to.

My (default, unchanged) prompt looks like this:
stephen@linux-7uwd:~>

Nice enough, I just want to add a bit of colour, and maybe some other, minor tweaks. but if I look at the $PS1 variable, I get this:

stephen@linux-7uwd:~> echo $PS1
\$(ppwd)\]\u@\h:\w>

So after a bit of reading, I work out what

\u@\h:\w> 

is doing, and more or less how it is doing it.
So some more migraine inducing googling, I learn that $(ppwd)] refers to some function alias or command which is performed first. So I:

stephen@linux-7uwd:~> type ppwd
ppwd is a function
ppwd () 
{ 
    local dir;
    local -i width;
    test -n "$TS1" || return;
    dir="$(dirs +0)";
    let width=${#dir}-18;
    test ${#dir} -le 18 || dir="...${dir#$(printf "%.*s" $width "$dir")}";
    if test ${#TS1} -gt 17; then
        printf "$TS1" "$USER" "$HOST" "$dir" "$HOST";
    else
        printf "$TS1" "$USER" "$HOST" "$dir";
    fi
}

Now; this is above my pay-scale so to speak, but I guess that somehow it stops the prompt from becoming too long and replacing the path with …/…/ (am I right in my guess?)

Oh, ok, I have answered my own question there… (No, it does not do that)

stephen@linux-7uwd:~/test/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep>

So I don’t know what it does. ~> echo $TS1 does not return anything, and I don’t know what || does or means. However, and whatever; let us press on! It was there by default, so it must be ok right?
Now in my painstaking research (!) I had come across some advice to put home-brewed aliases, functions and scripts in their own file, and then reference that file from .bashrc I have shamelessy lifted this advice and the code snippets that came with, and I have had some success, putting this in .bashrc:

if  -f ~/.bash_aliases ]; then
   . ~/.bash_aliases
fi

And I have put my experiments in .bash_aliases
And so at last to my question… Well done to anyone still here!

If I place in .bashrc or in my own .bash_aliases the set variable:

PS1="\$(ppwd)\]\e[0;31m\\u \A:\w> \e[m"

W00t! etc etc, I get a nice red prompt, and as a bonus a little reminder that it is much later than I thought and that I really really should do some proper stuff today, or at least get dressed… etc…

BUT BUT BUT! If I now carry on my researches in to the mystery that is BASH, I realise that now… If I use the up arrow to go back through my command history, I can go back a certain number of up arrows/cmds and then my prompt becomes amended with a remnant of some random command. If I then go back with the down arrow, this remnant remains, and the only way I have found to clear it is to ctl-C. This is not random, it is reproducible always, and stops when I comment out the PS1 setting.
If I up arrow 20 commands, I get the first 9 characters of that command becoming fixed to my cmd prompt, and cannot get rid of it. If I try to backspace to the > part of my prompt it goes as far as 9 characters of that cmd and stops there.
This has left me bewildered and depressed and looking for a GUI to do my back-ups. Or a MAC. Or something.

As a ps. (As if you wanted more of my rambling!) I googled up a similar problem and got a close result. Then I realised that it was a question posted by me FIVE YEARS AGO. openSuse then had a different function in ppwd, which I never got to understand either…

PS1 bash prompt

And what is it you want to achieve with this post? It’s posted in Applications, but I don’t get what you’re trying to say.

Hi
This bit…

BUT BUT BUT! If I now carry on my researches in to the mystery that is BASH, I realise that now… If I use the up arrow to go back through my command history, I can go back a certain number of up arrows/cmds and then my prompt becomes amended with a remnant of some random command. If I then go back with the down arrow, this remnant remains, and the only way I have found to clear it is to ctl-C. This is not random, it is reproducible always, and stops when I comment out the PS1 setting.
If I up arrow 20 commands, I get the first 9 characters of that command becoming fixed to my cmd prompt, and cannot get rid of it. If I try to backspace to the > part of my prompt it goes as far as 9 characters of that cmd and stops there.
This has left me bewildered and depressed and looking for a GUI to do my back-ups. Or a MAC. Or something.

I certainly don’t experience the phenomon you describe:


glosscomputer@laptop:~> export PS1="\$(ppwd)\]\e0;31m\\u \A:\w> \em" 
glosscomputer 15:39:~> asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd

Did you write “export” before PS1 in your bashrc? (Actually I’m not sure if that would matter)

Thank you Malcolm and Knurpht, for taking the time to read my rambling post. There was a reason for being so long-winded, but for now (for once!) I shall try to be brief :slight_smile:

I have noticed that in the example I posted above, and that Knurpht has kindly tested, that there is (at least) one error…

glosscomputer@laptop:~> export PS1="\$(ppwd)\]\e[0;31m\\u \A:\w> \e[m" 

The double backslash after the colour definition 0;31m\u Does this matter or do anything? Could it matter under ANY circumstances?

glosscomputer 15:39:~> asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd

I am not sure Knurpht, that you understood the problem that manifested itself on my machine after applying the code. It is not easy to demonstrate, but BASH history behaves in a very odd and wrong way after applying it, so that pressing the UP arrow to scrollback through the commands, after a certain number of command scrollbacks, messes up the prompt altogether, by affixing a remnant of a command to the prompt, which then cannot be removed, except by pressing ctrl-C. It is not possible for me to tell whether this is an entirely random number of presses of the up arrow, or that number is somehow generated by something. For instance, I rebooted this morning and was doing some stuff before coming here. If I go now to konsole, and hold down the up arrow, after some number of commands have scrolled back, the prompt has a portion or remnant of a command attached to it. In this present case,
cd /usr/sh

[IMG]http://i127.photobucket.com/albums/p145/wakou/bashstuck_zps4ebd9570.png

If I now backspace, to get to my ‘normal’ prompt, by erasing what command history has put there, it only backspaces as far as the remnant:

http://i127.photobucket.com/albums/p145/wakou/bashstuck2_zps561080a5.png

[QUOTE=Knurpht]Did you write “export” before PS1 in your bashrc? (Actually I’m not sure if that would matter)
[/QUOTE=Knurpht]

This, I did not do. I am now reading up on ‘export’ to see what differences it might make. If this is what makes it fail, I would still be interested to find out precisely why such strange and damaging behaviour would result from its omission, and would be very grateful if any of you could reproduce it on your machines.
Thanks again, and it is back to the man pages for me…

ps I have also noticed more mystifying behaviour, with the code as is: After reboot etc, with the above code in .bashrc, without the ‘export’, if I do

echo $PS1

it returns

echo $PS1
\\]\e[0;31m\u \A:\w> \e[m

ie the function call "[$(ppwd)] has disappeared, even though it is still in .bashrc

I am sorry it was a bit rambling and incoherent. As in the title, I did not know whether to put it in ‘Applications’. Is Bash an application? Did there not used to be a scripting sub-forum?

Please post content of .bashrc and .bash_aliases.

BTW: bash is a shell, but I tend to see it as a program/application. Let’s leave it here.

Edit: have you tried to set nothing but the prompt for a new user? I.e. have a clean default .bashrc, then export the PS1 statement. If things are normal for that new user, something else in your bash environment is wrong.

On 2013-08-30, wakou <wakou@no-mx.forums.opensuse.org> wrote:
>
> Knurpht;2581705 Wrote:
>> And what is it you want to achieve with this post? It’s posted in
>> Applications, but I don’t get what you’re trying to say.
> I am sorry it was a bit rambling and incoherent. As in the title, I did
> not know whether to put it in ‘Applications’. Is Bash an application?
> Did there not used to be a scripting sub-forum?

There still is a programming/scripting subforum.

Thanks Knurpht,
I will try as another user, and set up a wholly new user as well.

(I have commented out the loader to .bash_aliases in .bashrc, but have included it for completeness)

stephen 10:13:~> cat ~/.bashrc
# Sample .bashrc for SuSE Linux
# Copyright (c) SuSE GmbH Nuernberg

# There are 3 different types of shells in bash: the login shell, normal shell
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.
#
# NOTE: It is recommended to make language settings in ~/.profile rather than
# here, since multilingual X sessions would not work properly if LANG is over-
# ridden in every subshell.

# Some applications read the EDITOR variable to determine your favourite text
# editor. So uncomment the line below and enter the editor of your choice :-)
#export EDITOR=/usr/bin/vim
#export EDITOR=/usr/bin/mcedit

# For some news readers it makes sense to specify the NEWSSERVER variable here
#export NEWSSERVER=your.news.server

# If you want to use a Palm device with Linux, uncomment the two lines below.
# For some (older) Palm Pilots, you might need to set a lower baud rate
# e.g. 57600 or 38400; lowest is 9600 (very slow!)
#
#export PILOTPORT=/dev/pilot
#export PILOTRATE=115200
#if  -f ~/.bash_aliases ]; then
#    . ~/.bash_aliases
#fi
alias bum='echo this is the result of bum'

#PS1="\$(ppwd)\]\u@\h:\w>"

PS1="\$(ppwd)\]\e[0;31m\\u \A:\w> \e[m"
test -s ~/.alias && . ~/.alias || true

stephen 10:15:~> cat ~/.bash_aliases

alias bum='echo this is the result of bum'
function mycpu()
{
sudo watch -n 1 -t -c "sensors; sudo awk '"' {print "CPU " core++ ":", ($1)/1000, "Mhz"}'"' /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq";
}

PS1="\e[0;31m\[\]\u \A:\w> \e[m"

stephen 10:17:~> echo $PS1
\[\]\e0;31m\u \A:\w> \em

I found some time to read it all again, and have managed to duplicate the weird behaviour. Something must be wrong in the PS1 string.

Now have a look at this, try it

export PS1="\$(ppwd)\]\u \A:\w> "

the weird behaviour when arrowing up through the bash history is gone, so there must be something wrong in the coloring

And, here it is:

export PS1="\$(ppwd)\]\\e[0;31m\]\u \A:\w> \\e[0m\]"

You see the difference ? I looked at it from a programmer’s point of view and noticed there were some “” without matching “]”.

Thank you so much for doing this Knurpht. Coincidentally, someone else had spotted this at the same time, and I was frantically trying to set up my new user to test out the differences, before coming back here to save you the extra working on my behalf.

“I am disappoint!”
as they say on that internet… I took the original ‘method’ from a source I had up until now trusted as a useful resource… ‘The Geek Stuff’

                               4. Change foreground color of the prompt

Display prompt in blue color, along with username, host and current directory information

$ export PS1=“\e[0;34m\u@\h \w> \e[m”
[Note: This is for light blue prompt]

$ export PS1=“\e[1;34m\u@\h \w> \e[m”
[Note: This is for dark blue prompt]
\e - Indicates the beginning of color prompt
x;ym - Indicates color code. Use the color code values mentioned below.
\e[m - indicates the end of color prompt

From “The Geek Stuff”

Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie

I shall write to Ramesh to inform him of the code mistake, so that others don’t repeat it. It is astonishing that with his site being massively popular that it has not been corrected. There IS a note, comment #16 which points out the flaw…

It is typical of my computering experience that whenever I try to do even the simplest thing, I choose to do a 1:1,000,000 chance thing which breaks things. Perhaps geeko is trying to tell me to stop.

I think your sig. is particularly apposite in this instance:

Anything that can go wrong… will teach us