Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-16-2007, 05:42 PM   #1
LQ Newbie
Registered: Feb 2006
Posts: 7

Rep: Reputation: 0
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:

case "$1" in
        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\]";;
        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> \$ "
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
Old 01-16-2007, 07:43 PM   #2
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
Old 01-16-2007, 07:44 PM   #3
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
Old 01-16-2007, 07:57 PM   #4
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
Old 01-16-2007, 08:03 PM   #5
LQ Newbie
Registered: Dec 2006
Posts: 2

Rep: Reputation: 0
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


but by

source yourscript


. yourscript

There ya go.
Old 01-17-2007, 06:01 AM   #6
LQ Newbie
Registered: Feb 2006
Posts: 7

Original Poster
Rep: Reputation: 0
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.



environment variable, prompt, shell, shell script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash prompt problem bjharker Linux - Newbie 5 07-18-2006 02:50 PM
setting up scp alias under bash - parameter passing problem gjr Linux - Newbie 4 06-08-2005 06:38 AM
bash prompt problem akudewan Slackware 7 03-09-2005 07:21 AM
bash prompt problem jackpal Linux - Software 6 02-04-2004 11:37 AM
bash problem: no prompt xyfan Linux - General 6 11-25-2003 11:15 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration