RE: [newbie]Change the command line prompt

1999-07-30 Thread Griffin, Michael

here is a basic question.

how do i change the command line prompt from ["username"@localdomain
/"directory"]#or$ to something more minimal say just something like
$"/directory" or #"/directory" (depending on the user).  it is my
perceived understanding that the difference for the "#" sign and the "$"
sign is there to denote whether one is a user or root.

i would like to make this a global change for all users so i am guessing
that a ".rc" file will need to be edited.

all help is greatly appreciated.

thanx,
michael



RE: [newbie]Change the command line prompt

1999-07-30 Thread Richard Myers


On Fri, 30 Jul 1999, Griffin, Michael wrote:
 how do i change the command line prompt from ["username"@localdomain
 /"directory"]#or$ to something more minimal say just something like
 $"/directory" or #"/directory" (depending on the user).  it is my
 perceived understanding that the difference for the "#" sign and the "$"
 sign is there to denote whether one is a user or root.
 i would like to make this a global change for all users so i am guessing
 that a ".rc" file will need to be edited.
 all help is greatly appreciated.
 thanx,
 michael

   (this is a quickie canned answer-- adapt as needed)

Here is some info about your BASH command prompt, and about the startup
sequence of your shell.

The command prompt is stored in the PS1 variable. You can type "set" and
you will see a list of all your current variable settings. The PS1
variable could be set in a number of places-- either a global startup
file, or an individual user startup file.

One setting for username is:

  PS1="\u-- "

The current directory is:

  PS1="\w-- "

If you combine \u and \w, you get both.

The other stuff-- in other words, the --  can be whatever you wish.
The trailing space is a nice thing to have, however.

S--- you can change easily by modifying a bash startup file.

Which file?

Well, first go to the command prompt and find out if it is already 
being set somewhere.

First check for the prompt in a global startup file. This may be 
/etc/profile, or possibly /etc/bashrc.

I'm currently using a Sun, but your Linux experience should be similar.

We are going to use the grep command: 

  grep PS1 /etc/*

...from a command prompt. Note that where I have just a $, you may have
something different:

$ grep PS1 /etc/*
grep: can't open /etc/initpipe
grep: can't open /etc/oshadow
/etc/profile:PS1=""
/etc/profile:PS1="#"
/etc/profile:export PS1
grep: can't open /etc/shadow
grep: can't open /etc/utmppipe
$

The first part of the line shows the filename. Ignore the errors.

Aha! My PS1 variable is set initially in the /etc/profile file.
This means that it is set to this value for everyone on the system,
because any file in the /etc directory is likely a global setting.
(But each user can set it again to something different.) 

Notice that the actual, global system code doesn't show up with 
grep-- we only see lines that include the "PS1" string of
characters. 

If we actually look at (my) /etc/profile file, we see this:

PS1=""
if [ "$LOGNAME" = "root" ]
then
PS1="#"
fi
export PS1

Well, I spoke too soon. My $ prompt doesn't match this code, so I have to
look further. Actually, my PS1 is redefined in my .profile file:

$ grep PS1 ~/.*
.bash_history:grep PS1 *
.profile:PS1="$ "
.profile:export PS1 PS2
$

Ignore the .bash_history entry. Note that for the BASH shell, your value
for PS1 will be slightly different from my (Sun) prompt.

If you are logging in as root, you get the # in your prompt instead of the
$ . But all that is needed to set my user prompt would be:

   PS1=  (something)

...and, so that the change is propagated to all shell logins,

   export PS1

You may wish to check the individual user files too-- each user can
customize their own PS1 variable. To see if it is set up in your user
startup files, type:

  cd
  grep PS1 ~/.*

...where ~/ (if it is set up properly) is your home directory, and .* is a
wildcard for searching all filenames that start with a period.

The three likely files to set the PS1 variable in your home directory are
.bash_profile, .bashrc, and .profile.

The difference between .bash_profile and .bashrc? Having two files gives
you the flexibility to separate startup commands needed at login time
(.bash_profile) from those you might need when you run a subshell
(.bashrc). So, I'd change .bash_profile for a user account (although you
could have different settings for subshells, too.)

Global files in /etc are run first, and then the user files in the home
directory are run. The last file to set and export PS1 will rule.

I'd try changing PS1 in a user login first, if you haven't had much
experience with editing text files in UNIX/Linix. Open your .bash_profile
with commands such as:

  cd
  pico .bash_profile

(substitute your favorite editor for pico, but pico is very easy to
use...)

Find out where variables are set (if you already have a .bash_profile-- it
is possible to use the /etc/profile file for all setup).

Change the PS1 variable to what you want.

Don't forget to export it!

Some other settings for PS1:

  \d   date
  \H   hostname
  \h   abbreviated hostname
  \n   a carriage return and linefeed
  \T   time
  \t   time, another format
  \@   time, yet another format
  \u   user name
  \v   version of bash
  \V   release version of bash
  \w   current working directory
  \W   abbreviated current working directory
  \#   command number of the current prompt
  \!   command history number of the current prompt