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…