Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Bash script help
New on LowEndTalk? Please Register and read our Community Rules.

Bash script help

geodirkgeodirk Member

I'm trying to just modify my bash prompt as part of a script. I'm wanting to change the default PS1= line in the .bashrc file and I'm stuck. This is what is in the default .bashrc file for that line:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ '
fi

I want to have my script change that line to:

PS1='\[\e]0;\w | \[email protected]\h\a\][\[\e[1;31m\]\[email protected](CentOS 5.9 32bit)\[\e[33m\]\W\[\e[0m\]]# '

So in my script, I've tried using this line:

sed -i "s/PS1=.*/PS1='\[\e]0;\w | \[email protected]\h\a\][\[\e[1;31m\]\[email protected](CentOS 5.9 32bit)\[\e[33m\]\W\[\e[0m\]]# '/g" /root/.bashrc

But all the \ escape characters get hosed and tubes it. So I tried escaping the escape codes like this:

sed -i "s/PS1=.*/PS1='\\[\\e]0;\\w | \\[email protected]\\h\\a\\][\\[\\e[1;31m\\]\\[email protected](CentOS 5.9 32bit)\\[\\e[33m\\]\\W\[\\e[0m\\]]# '/g" /root/.bashrc

Still doesn't help. Any ideas?

And yes, I know that with the way that I wrote the sed command that it will change ALL of my PS1= strings in the whole file. I'm OK with that.

Sarcasm: Just one more service I offer

Comments

  • I usually use single quotes around sed commands, and quote any single quotes I need within the string.

    Do you need to edit the bashrc in-place? Can you not add a line to source all files in a custom bashrc.d directory and then change PS1 in a script there? You could also use a bash escape (\h or something) for the hostname, and a command escape $() for the OS.

  • Don't have time to really read through the whole thing, but one more thing I can add to above is that with sed you don't have to use '/' as you separator. You can for example do: sed -i 's+something+somethingelse+g'. You can use many different character besides '/' just look it up in the manual. While not a fix to your problem might help you a bit with your hunt.

    If I have some more time later and no one else has come up with the resolution I may dig into it some more.

    Cheers!

    Have an Allwinner H3 device? Check out H3Droid! | Lichee Pi Zero - The 6$ SBC | #SYSarm - Get It! | Armbian | Atomic Pi Mirror
    22+ Years IT Experience in Linux/Windows Hosting, Administration and Development Services

  • raindog308raindog308 Administrator, Moderator

    TheLinuxBug said: Don't have time to really read through the whole thing, but one more thing I can add to above is that with sed you don't have to use '/' as you separator. You can for example do: sed -i 's+something+somethingelse+g'. You can use many different character besides '/' just look it up in the manual. While not a fix to your problem might help you a bit with your hunt.

    In my time I've seen people get extremely far down into the backslashitis rabbit hole just because they never learned this. Once you stop using slashes as delimiters, things are a lot easier.

    For LET support, please visit the support desk.

  • Amen to changing the separator; it's a useful trick.

    I'm not sure if CentOS uses /etc/bash/bashrc.d/ , but if so you can just drop a script in there overriding PS1, and you don't need to edit the main bashrc in-place, avoiding the backslash mess. Manage the add-in with ansible, and you're good to go.

    If you have /usr/bin/lsb_release installed, you can use something like $(lsb_release -si) to get the distro name.

  • edited April 2016

    @geodirk

    Try this:

    sed -i 's/PS1=.*/PS1=\x27[\e]0;\w | \[email protected]\h\a][[\e[1;31m]\[email protected](CentOS 5.9 32bit)[\e[33m]\W[\e[0m]]# \x27/'

Sign In or Register to comment.