LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem setting new prompt on bash (https://www.linuxquestions.org/questions/linux-newbie-8/problem-setting-new-prompt-on-bash-519956/)

elbarto 01-16-2007 05:42 PM

Problem setting new prompt on bash
 
Hey guys, it's been a while since my last post...

Anyway, I was foolin around with my slackware and wanted to make a little script to change prompts according to my mood.

Ok, here's what I did:

Code:

#!/bin/bash
case "$1" in
    "rose")
        PS1="\033[0;35m\]@\033[0;32m\]}--}-\033[1;37m\]\u\033[0;31m\]@\033[1;37m\]\h\033[0;32m\]-{--{\033[0;35m\]@ \033[0;0m\]";;
    "reagge")
        PS1="\033[0;32m\]<\033[0;0m\]---\033[0;31m\]\u\033[1;33m\]@\033[0;31m\]\h\033[0;0m\]---\033[0;32m\]>\033[0;0m\]$ ";;
        *)
        PS1="<\u@\h> \$ "
    esac
export PS1

Inside the script everything works. The case statement works, the $PS1 var is correctly set. THe problem is that it doesn't change my prompt. What should I do besides (or instead of) exporting the variable?
Is it somehow impossible what I'm trying to do?

I'll appreciate any help. Thx

anomie 01-16-2007 07:43 PM

You can't export a variable to the parent (shell) that you called the script from.

A different possibility would be to somehow randomize or select per day of the week which PS1 options you're going to use, and then put that in ~/.profile or ~/.bash_profile.

bigrigdriver 01-16-2007 07:44 PM

It's odd that your prompt doesn't change. I just copied one of your prompts into a console. As soon as I pressed <enter>, my prompt changed.

Perhaps it's a Slackware config of console behavior that you need to look into.

anomie 01-16-2007 07:57 PM

Quote:

I just copied one of your prompts into a console.
Copying one of his prompts should work fine. But he's calling a new shell interpreter. The env variable only lives as long as the script does.

Try making a new file and copying his entire script in. Then call the script and see what happens.

dolsson5 01-16-2007 08:03 PM

source it!
 
They're right, you can't execute a program and (easily) change environment variables in the parent program. If you launch a #!/bin/bash script, it runs under its own instance of bash, not the one you started it from. So the PS1 value was good for the bash instance running the script, but once that finished and you returned to your original bash, you still had your original PS1.

Solution is to run the script under your original bash instance. Do that not by launching your script in the usual way, but by 'sourcing' it into the current shell. In other words, not by

./yourscript

but by

source yourscript

or

. yourscript

There ya go.

elbarto 01-17-2007 06:01 AM

Thanks to all, specially to dolsson5.
That was exactly the answer I was looking for. I hadn't realized that I was opening a new bash instance. I did as you said and it's working now.

Thx


All times are GMT -5. The time now is 08:33 PM.