# [sh] PS1 setting problem



## ivand58 (Sep 23, 2010)

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: 

```
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 ?


----------



## SirDice (Sep 23, 2010)

Remove the hash (#) everything after it is a remark.


----------



## anomie (Sep 23, 2010)

That, or I think OP meant to put the hash within the single quotes.


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

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


----------



## ivand58 (Sep 23, 2010)

anomie said:
			
		

> That, or I think OP meant to put the hash within the single quotes.
> 
> 
> ```
> ...



Thanks, I see, but it still doesn't work. Probably the 'export PS1' doesn't affect the parent's environment ?


----------



## Beastie (Sep 23, 2010)

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.

```
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))


----------



## ivand58 (Sep 23, 2010)

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.


----------



## anomie (Sep 24, 2010)

Don't execute it, _source it_ in. See an example in post #5.


----------



## jalla (Sep 24, 2010)

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.
> 
> ```
> 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).


----------



## jasmine (Sep 24, 2010)

Try 
	
	



```
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.


----------



## phoenix (Sep 24, 2010)

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:
> ...



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

This is taken from the bash man page:

```
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:

```
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`


----------



## ivand58 (Oct 17, 2010)

thanks, this is the solution:


			
				phoenix said:
			
		

> ... Then, once logged in, just source that into your running shell:  `$ . .profile.username1`


----------



## bu_mi (Oct 5, 2021)

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


----------



## Vull (Oct 5, 2021)

For /bin/sh shell users only:

Starting with FreeBSD version 13.0, you must modify ~/.shrc because ~/.profile contains these lines:
	
	



```
# set ENV to a file invoked each time sh is started for interactive use.
ENV=$HOME/.shrc; export ENV
```

... and ~/.shrc contains these lines:
	
	



```
# 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.


----------



## SirDice (Oct 5, 2021)

bu_mi said:


> ```
> case `id -u` in
> 0) PS1="${PS1}# ";;
> *) PS1="${PS1}$ ";;
> ...




```
\$      Superuser status.  “$” for normal users and “#” for
                           superusers.
```
See sh(1)


----------

