Page 1 of 2 12 LastLast
Results 1 to 10 of 13

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

  1. #1
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,085

    Default 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:




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

    So after a bit of reading, I work out what
    Code:
    \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:
    Code:
    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)
    Code:
    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:
    Code:
    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:


    Code:
    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

    Tumbleweed/KDE/Ati Radeon R9 270/AMD FX-6300/8Gb DDR3

  2. #2
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,793
    Blog Entries
    14

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

    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.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,669
    Blog Entries
    15

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

    Quote Originally Posted by Knurpht View Post
    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.
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  4. #4
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,793
    Blog Entries
    14

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

    I certainly don't experience the phenomon you describe:
    Code:
    glosscomputer@laptop:~> export PS1="\[$(ppwd)\]\e[0;31m\\u \A:\w> \e[m" 
    glosscomputer 15:39:~> asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd
    Did you write "export" before PS1 in your bashrc? (Actually I'm not sure if that would matter)
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  5. #5
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,085

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

    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

    I have noticed that in the example I posted above, and that Knurpht has kindly tested, that there is (at least) one error..
    Code:
    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?

    Code:
    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



    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:





    [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
    Code:
    echo $PS1
    it returns
    Code:
    echo $PS1
    \[\]\e[0;31m\u \A:\w> \e[m
    ie the function call "\[$(ppwd)\] has disappeared, even though it is still in .bashrc

    Tumbleweed/KDE/Ati Radeon R9 270/AMD FX-6300/8Gb DDR3

  6. #6
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,085

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

    Quote Originally Posted by Knurpht View Post
    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?

    Tumbleweed/KDE/Ati Radeon R9 270/AMD FX-6300/8Gb DDR3

  7. #7
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,793
    Blog Entries
    14

    Default Re: BASH, $PS1 and some other rambling. (BASH an application? There 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.
    Last edited by Knurpht; 30-Aug-2013 at 01:53.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  8. #8

    Default Re: BASH, $PS1 and some other rambling. (BASH an application? Thereused to be a scripting sub-forum?)

    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.

  9. #9
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,085

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

    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)

    Code:
    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
    Code:
    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"
    Code:
    stephen 10:17:~> echo $PS1
    \[\]\e[0;31m\u \A:\w> \e[m

    Tumbleweed/KDE/Ati Radeon R9 270/AMD FX-6300/8Gb DDR3

  10. #10
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,793
    Blog Entries
    14

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

    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.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •