Bash conditional prompt?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Bash conditional prompt?
# 1  
Old 08-01-2010
Bash conditional prompt?

Hi,

Does anyone know any way of making bash prompt extended with conditional content?

Example:

Code:
export PS1="[[ ${USER} = 'root' ]] && echo '#' || echo '\$'" # This won't work - prompt is not executed
# export PS1="\$" # This is an existing but also working equivalent

I would like to use more complex conditionals than in the given example.
# 2  
Old 08-01-2010
My main system's /etc/profile contains the following:

Code:
case $UID in
0)
  export PS1="\[\e[31m\][root@leonov]\[\e[0m\] "
  export PS2="\[\e[31m\][...........]\[\e[0m\] "
;;
1000)
  export PS1="\[\e[32m\][house@leonov]\[\e[0m\] "
  export PS2="\[\e[32m\][............]\[\e[0m\] "
;;
esac

# 3  
Old 08-01-2010
Just to be clear, no, you can't put it in the variable itself.
# 4  
Old 08-01-2010
you can try Smilie
Code:
[[ "$(id -un)" == "root" ]] && export PS1='#' || export PS1='\$'

# 5  
Old 08-01-2010
I am afraid that the answers given are not correct. There is a major difference between
conditional content in the prompt
and
conditional logic in the script that sets that prompt

The answer given by Corona688 seems to be the only one related.

Regards
# 6  
Old 08-01-2010
Quote:
Originally Posted by adderek
I am afraid that the answers given are not correct. There is a major difference between
conditional content in the prompt
and
conditional logic in the script that sets that prompt

The answer given by Corona688 seems to be the only one related.

Regards
As I understood it, Corona688 was only confirming what dr.house, and later ygemici had said, just more directly.

Given that, presenting alternatives was perfectly correct.
# 7  
Old 08-02-2010
Both Kshell and bash expand variables at the time PS1 is written to the tty, and thus using $() the shell can be coaxed into doing what you want:

Code:
PS1='$(if [[ $USER == root ]]; then  echo "$PWD#"; else echo "$PWD%"; fi)'


I agree with earlier posts that suggest setting the prompt either in your profile, or the system profile, as being the smart way of doing it, but wanted to point out that it can be done this way.
These 3 Users Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Autojump with modified bash prompt

I use the .bashrc file from this thread. Direct link to the archive containing the ultimate bashrc I am trying to use autojump, but it will not autoload the directories to the autojump list as the custom prompt is not compatible. Here is the thread on the issue from other users. apt -... (2 Replies)
Discussion started by: genehunter
2 Replies

2. Shell Programming and Scripting

Conditional bash/mysql query help

I think(hope) I've got a simple one - I just need to send an email if a mysql query returns any results (ideally - it will never match). Currently I just pipe the mysql query output to the mail program, but of course that emails regardless of the output( and I check this every 10 minutes from... (5 Replies)
Discussion started by: jcass78
5 Replies

3. UNIX for Dummies Questions & Answers

Bash prompt is over lapping

Hello Guys, I have facing problem with linux shell prompt .Am expecting my Bash prompt to be like below but its showing like ~]$ ot@Servername and while typing the commands the prompt looks like below: ~]$ echo $PS1 $ ~]$ ot@ServernameChecked the .bash_profile and also changed... (9 Replies)
Discussion started by: kapil514
9 Replies

4. Shell Programming and Scripting

Bash conditional | getting logic wrong?

I have a file cat <<EOF > /tmp/test Line one Line two Line three EOF Now I am trying to get the exit stat ($?) of 1 if any text is found and 0 if any text is not found using grep, i.e. just reversing the exit status of grep # (snippet 1) this one is not working!!! retval $?... (4 Replies)
Discussion started by: the_gripmaster
4 Replies

5. Emergency UNIX and Linux Support

Bash answer prompt

I am working with a script to simplyfy some operations where I work, but one of the programs needs me to enter a password. It will as me "Please enter the administrator password:" Is there a way to make a bash script to automatically answer the question with the needed password? I am looking... (3 Replies)
Discussion started by: noratx
3 Replies

6. Shell Programming and Scripting

BASH: Getting titlebar and prompt to 'sync up'

This is an instance of "if it's not one thing, it's another." I recently fell victim to my own stupidity in trashing, by accident, my long-running and very highly-customized .bash_profile and .bashrc files for Cygwin & Cygwin/X. I had backups from a previous "go" with this, and decided to use... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

7. OS X (Apple)

Bash prompt wraparound

I'm using a custom prompt with PS1 in my .profile. It is PS1="\\u@\e\:\W\ \\$\ \" and it works well, as you can see, http://content.screencast.com/users/killer54291/folders/Jing/media/2b3db52a-ebf7-43e2-95cc-f45dadbc2b98/00000023.png but, when i type more than the width of the window, it... (0 Replies)
Discussion started by: killer54291
0 Replies

8. Shell Programming and Scripting

How can i change my bash prompt ?

It looks like, user@hostname:/auto/home3/user$ Desired, user@hostname$ I added following line in .bashrc, but still its same. export PS1=" $ " Please help me :confused: (13 Replies)
Discussion started by: admax
13 Replies

9. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

10. UNIX for Dummies Questions & Answers

customize my prompt in bash

in csh I was using: set prompt=""$HOSTNAME".tk.\!> " to customize the look of my prompt. I have seen the light after reading the perils of csh scripting and wish to switch to bash. How do I customize my bash prompt??? I've tried many variation of the above w/no success, and searching this... (2 Replies)
Discussion started by: yankee428
2 Replies
Login or Register to Ask a Question