[sh] PS1 setting problem

Hi list,

Is there a way to set the command prompt $PS1 with shell scrypt? I can't use .profile because we are few users on one account and each user prefers different PS1.
I tried with file.sh:
Code:
PS1=`uname -n`:'$PWD'# ; export PS1
but after the execution of file.sh, PS1 remains the same ...

So, how can I set PS1 with shell scrypt ?
 
That, or I think OP meant to put the hash within the single quotes.

Code:
PS1=`uname -n`:'$PWD#' ; export PS1

If so, I'd discourage the idea - hash on a prompt is a visual cue to signify root. :)
 
anomie said:
That, or I think OP meant to put the hash within the single quotes.

Code:
PS1=`uname -n`:'$PWD#' ; export PS1

If so, I'd discourage the idea - hash on a prompt is a visual cue to signify root. :)

Thanks, I see, but it still doesn't work. Probably the 'export PS1' doesn't affect the parent's environment ?
 
ivand58 said:
Thanks, I see, but it still doesn't work. Probably the 'export PS1' doesn't affect the parent's environment ?
Are you reloading ~/.profile after changing it:$ . .profile?

Also, remove the apostrophes or $PWD will be displayed as is, without being expanded. As anomie already mentioned, a dollar sign is used by convention for users and a hash for root.
Code:
PS1=`uname -n`:$PWD$ ; export PS1

----

SirDice said:
Remove the hash (#) everything after it is a remark.
The character `#' introduces a comment if used at the beginning of a word. The word starting with `#' and the rest of the line are ignored.
(source: sh(1))
 
thanks, but the problem is not in the # nor in the apostrophes. the script is OK (proved by $ echo $PS1).

The problem is that after the execution of the script, PS1 remains the old one.
 
Beastie said:
Also, remove the apostrophes or $PWD will be displayed as is, without being expanded. As anomie already mentioned, a dollar sign is used by convention for users and a hash for root.
Code:
PS1=`uname -n`:$PWD$ ; export PS1
But realize this will put in the fixed path where you happen to be when the code is sourced. It's probably not what OP is trying to do (which isn't possible with /bin/sh AFAIK).
 
Try
Code:
PS1='\H:\w \$ '

They're undocumented (from parser.c) though.

You can write a few functions/aliases to change PS1 and source them from ENV for convenience.
 
ivand58 said:
Hi list,

Is there a way to set the command prompt $PS1 with shell scrypt? I can't use .profile because we are few users on one account and each user prefers different PS1.
I tried with file.sh:
Code:
PS1=`uname -n`:'$PWD'# ; export PS1
but after the execution of file.sh, PS1 remains the same ...

So, how can I set PS1 with shell scrypt ?

For which shell? sh(1) bash(1) zsh(1)

This is taken from the bash man page:
Code:
PROMPTING
       When executing interactively, bash displays the primary prompt PS1 when
       it is ready to read a command, and the secondary  prompt  PS2  when  it
       needs  more  input  to  complete  a  command.  Bash allows these prompt
       strings to be customized by inserting  a  number  of  backslash-escaped
       special characters that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the  date  in "Weekday Month Date" format (e.g., "Tue May
                     26")
              \D{format}
                     the format is passed to strftime(3)  and  the  result  is
                     inserted  into the prompt string; an empty format results
                     in a locale-specific time representation.  The braces are
                     required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the  name  of  the shell, the basename of $0 (the portion
                     following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the  current  working  directory,  with $HOME abbreviated
                     with a tilde
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin  a sequence of non-printing characters, which could
                     be used to embed a terminal  control  sequence  into  the
                     prompt
              \]     end a sequence of non-printing characters

This will work in all of the sh-based shells listed above, although sh may not support all of them.

If using bash or zsh, you can edit the .profile or .bashrc or .zshrc with something like:
Code:
if [ ${USER} -eq "username1" ]; then
  PS1=blahblahblah
elif [ ${USER} -eq "username2" ]; then
  PS1=somethingelse
fi

Or, if you are all logging in to the same account (WHY????), then you can create separate files (.profile.username1; .profile.username2; etc) with the PS1 settings. Then, once logged in, just source that into your running shell: $ . .profile.username1
 
In case someone ends up here decades later like I did (thanks to the parser.c link above from Jasmine and a bashrc file from a Linux machine) the following works for me in my .bashrc file on FreeBSD:

PS1="[\u@\h:\W] "
case `id -u` in
0) PS1="${PS1}# ";;
*) PS1="${PS1}$ ";;
esac
 
For /bin/sh shell users only:

Starting with FreeBSD version 13.0, you must modify ~/.shrc because ~/.profile contains these lines:
Code:
# set ENV to a file invoked each time sh is started for interactive use.
ENV=$HOME/.shrc; export ENV

... and ~/.shrc contains these lines:
Code:
# set prompt: ``username@hostname:directory $ '' 
PS1="\u@\h:\w \\$ "

In other words, by default, ~/.shrc will run after ~/.profile and supersede any changes to PS1 you might have made there.
 
Back
Top