Visit Our UNIX and Linux User Community

[Solved] Help needed to have changing value to the command prompt string variable PS1

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Help needed to have changing value to the command prompt string variable PS1
# 1  
Old 04-25-2012
[Solved] Help needed to have changing value to the command prompt string variable PS1


I am using git bash terminal window to do git operations. I have set the prompt string variable PS1 in the ~/.bashrc file as follows:
export PS1="[$PWD $(git branch | grep '*') $] "

This is intended to show me the current git branch's name which is active as part of the prompt string. But, the problem is when I do a git checkout command to change to some other branch in the terminal, this variable's value is not getting updated and still showing the old one.

Please help me with your suggestions.

Last edited by royalibrahim; 04-26-2012 at 02:50 AM..
# 2  
Old 04-25-2012
with this double quoting, look at PS1 after assignment:

$ PS1="[$PWD $(git branch | grep '*') $] "
[/home/mute  $] declare -p PS1
declare -- PS1="[/home/mute  \$] "

Your variables are expanded inside of a double-quote before assignment. If you escape the $ then it will be stored inside of PS1. You can also use single quotes to avoid expansion, but you have some in there so those are the 1 character you need to treat special inside single quotes:

$ PS1='[$PWD $(git branch | grep '\''*'\'') $] '
fatal: Not a git repository (or any of the parent directories): .git
[/home/mute  $]

Having a command execute for each prompt is a bit much. Also maybe you'd like to make it a function and handle those fatal errors in case you leave a git branch.

[/home/mute/code  $] PS1='[$PWD $(git branch 2>/dev/null | grep '\''*'\'') $] '
[/home/mute/code  $] cd Twitter.awk/
[/home/mute/code/Twitter.awk * master $]

et voila!?
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 04-26-2012
Amazing.. truly mind blowing suggestion!! thanks a ton neutronscott Smilie, worked like a charm

Previous Thread | Next Thread
Test Your Knowledge in Computers #386
Difficulty: Medium
The Linux telnet command provides a user interface to a remote system using the SSH protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Help changing the PS1 prompt in Solaris

Hi, I need help changing PS1 in Solaris. I tried this: MYPROMPT="> " PS1=$LOGNAME@$HOSTNAME:${PWD}$MYPROMPT (NOT SURE WHY IT'S HIGHLIGHTED HERE) export PS1 My problem is that $PWD is not working, when I get the prompt and I change directories, the prompt is not displaying the current... (17 Replies)
Discussion started by: curiousmal
17 Replies

2. UNIX for Dummies Questions & Answers

Need to revert default prompt in Linux after setting PS1 command

I have given as: PS1="Karthick>" in linux. Now the prompt changed as: Karthick> Now I need to get back the default prompt . How to achieve this? Thanks in advance (13 Replies)
Discussion started by: karthick nath
13 Replies

3. UNIX for Dummies Questions & Answers

Why is my PS1 breaking my prompt?

So, this is strange... I created this prompt: PS1='\n\e You can see that it's a pretty minor modification of the default Debian prompt. And, if it matters, I'm using Putty to SSH to my server. The following strange symptoms appear when I use that prompt, and disappear when I change and... (2 Replies)
Discussion started by: treesloth
2 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Variable Name as a Prompt

Dear Members, I have an variable by name dir.If i do echo $dir i will get the path (/usr/bin/). I am writing a shell script which will prompt to enter the variable name if run.Suppose the script name is If run it will prompt for entering variable name which is dir.Suppose... (9 Replies)
Discussion started by: sandeep_1105
9 Replies

5. Shell Programming and Scripting

How to get a Prompt (PS1) Timestamp under /sbin/sh?

Hi, I'm trying to find out if there is a way to get a timestamp on my Solaris root shell prompt using /sbin/sh? I'm trying to archive something in line with the following: 12:34:26 root@server # 12:34:28 root@server # 12:34:28 root@server # ls ... 12:34:30 root@server # I know there... (1 Reply)
Discussion started by: Solarius
1 Replies

6. UNIX for Dummies Questions & Answers

PS1 prompt

please advise what's wrong with this command ? PS1="`hostname`:`who am i | cut -d " " -f1`:>>" trying to make the PS1 prompt look like : machine_name:username:>> thank you (4 Replies)
Discussion started by: venhart
4 Replies

7. UNIX for Dummies Questions & Answers

colors in Prompt - $PS1

would someone please explain in detail, how does the code below change the color or bash prompt $ echo $PS1 :\033 are there other tricks like above? (3 Replies)
Discussion started by: rakeshou
3 Replies

8. Shell Programming and Scripting

Changing text in the command prompt

Hi, I want to change my command prompt to contain the current username and the current directory in it, instead of just the '$' symbol. I tried the command:- export PS1="$(echo \\n$) " But whenever I switch the user or change the directory, the changes are not reflected in the command... (10 Replies)
Discussion started by: unipepper
10 Replies

9. UNIX for Dummies Questions & Answers

Very simple question about changing PS1 variable at startup!

Hello there ! I am new in this Unix world and just start learning Unix. I have very simple question about changing PS1 variable (Shell Prompt) i have local.profile file in my working directory, i open in vi edit mode and add this line PS1="Hello:>" and i save that file. I disconnected from... (2 Replies)
Discussion started by: abidmalik
2 Replies

10. UNIX for Dummies Questions & Answers

Changing the UNIX command prompt

I am having a hard time figuring out how to change the command prompt in my UNIX shell. I am using the bash shell, and I would like to set the prompt to show me the full path of the current working directory along with my username, I suppose... The main thing I want is the full path of the... (2 Replies)
Discussion started by: WERUreo
2 Replies

Featured Tech Videos