How to show hostname infor after login

When I login AIX console, how to show hostname before any command:

hostname:/#>id
uid=0(root) gid=0(system) groups=2(bin),3(sys),7(security),8(cron),10(audit),11(lp)

As illustrated above with the colon-slash-hash-greaterthan, like so:

PS1="$(hostname):/#>"

Not sure on AIX but on linux you can do:

PS1="\H/#"

Where:

       \a     an ASCII bell character (07)
       \d     the  date  in  "Weekday  Month  Date" format
              (e.g., "Tue May 26")
       \e     an ASCII escape character (033)
       \h     the hostname up to the first `.'
       \H     the hostname
       \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
       \u     the username of the current user
       \v     the version of bash (e.g., 2.00)
       \V     the  release  of  bash, version + patchlevel
              (e.g., 2.00.0)
       \w     the current working directory
       \W     the basename of the current  working  direc-
              tory
       \!     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 con-
              trol sequence into the prompt
       \]     end a sequence of non-printing characters

Followup: If your hostname is fully qualified (e.g. node.coname.com) you can do the following to just get node:
PS1="$(hostname|awk -F. '{print $1}'):/#>"

Also, if you want the current path as part of the name, you can do this:

P='$PWD' # that is single tick (apostrophe)
PS1="$(hostname|awk -F. '{print $1}'):${P}/#>"

We use a "$" when we are logged in as root:

PS1=`hostname`:'${PWD#${PWD%/*/*/*}/}:$ '

And use a "#" when we are logged in as a non-root user:

PS1=`hostname`:'${PWD#${PWD%/*/*/*}/}:# '

I can see very quickly if I am root or not by looking for the "$".

Thats odd. All my servers, by default, are $ for nonroot and # for root.

Yeah, that is probably the better way to do it since most Linux distributions do it that way also. It has just been switched on our boxes for as long as I can remember so we just leave it at that.

Either way, it is good to have the root PS1 set differently that the non-root PS1 just so you can tell quickly if you are root or not.

I entered the following, expecting to get "hostname:/current/path> " as a prompt (based on your post), but look what happened:

/msa1/agility/devel/bin> PS1="\h:\w> "
h:w> uname -s
Linux
h:w>

Did I misunderstand something?
~Marcus

for AIX, put the sytax as below into your .profile

# Set prompt to display login@hostname:current directory

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

Or for all users, put sytax into /etc/profile as below. It will let all users prompt host name and current directory.

# Set the primary prefix prompt to display machine name and current directory
if [ "`id | grep '^uid=0('`" != "" ]
then
PS1="[`uname -n`:"'$'PWD"] # "; export PS1
else
PS1="[`uname -n`:"'$'PWD"] $ "; export PS1
fi

Yes, I'm familiar with doing it that way. I was intrigued by the control character construct (e.g., \h \w etc.) and wanted to see that work. That is what I was having trouble with.

I did it on the command line. Is there some special processing that happens in .profile? In other words, if I put the same statement (PS1="\h:\w> ") in my .profile, does it process the special control characters? (I cannot imagine that is the case.)

Are you using csh? ksh and bash both seem to "see" the changes to PS1 immediately. (i.e. without having to export them). What shell are you using?

ksh.
This works fine on the command line:

$ D='$PWD'
$ PS1="$(hostname -s):$D> "
strawboss:/home/mlibby> cd /msa1/profiles/mlibby/ksh
strawboss:/msa1/profiles/mlibby/ksh>

The problem I'm having is when I'm trying to see the control characters work. According to an earlier post, I should be able to use "\h" for short host name--in lieu of $(hostname -s); I should be able to use "\w" for working directory--in lieu of D='$PWD' and $D.

I know how to get the prompt I want. The control character (back-slash construct) was new information to me, and I was trying to get that to work. That is what I'm having difficulty with. According to the earlier post in this thread (if I read it right), I should be able to type this to get the same results as above:

$ PS1="\h:\w> "
h:w>

... but as you see, instead I get the prompt "h:w>"
~Marcus

ah, I see what you mean. As I only have a Linux machine here, I can't say for "real" ksh but pdksh does not understand \u@\h: \w escapes for the PS1 variable. They appear to only apply to bash.

Having just looked it up, the escape sequences only applie to bourne type shells not to korn or csh. To have dynamic, colourful prompts with those shells you are going to have to be more creative. (chapter 4 of "Unix Power Tools" has a very good section on prompts).
UNIX Power Tools - Google Book Search

Thanks wempy. I missed the part about it only working with bourne/bourne-again in the initial post. Thanks.

When you log on to an AIX workstation you have a ksh (actually ksh88) as default login shell. Use a ksh manual if you need more information than provided here - everything in there applies to your AIX prompt as well.

I dislike the long prompts because they leave little space to type commands. As i usually work in an X environment i exploit some properties of the usual X-terminal-emulators and set the prompt with the following scriptlet to display hostname, user and PWD in the title bar. Notice that "^[" in the following text stands for a literal <ESC> character (in vi press <CTRL>-<V> to enter a control character, then press <ESC>, similar for "^G" [literal CTRL-G] and "^M" [literal CTRL-M]):

case $TERM in
     aixterm)
          PS1='^[];$LOGNAME@$HOST:$PWD^G^M# '
          ;;

     dtterm)
          PS1='^[]0;$LOGNAME@$HOST:$PWD^G^M# '
          ;;

     xterm)
          PS1='^[];$LOGNAME@$HOST:$PWD^G^M# '
          ;;


     vt100|ansi)
          PS1='$IFS$LOGNAME@$HOST:$PWD$IFS# '
          ;;

     *)
          PS1='$IFS$LOGNAME@$HOST:$PWD$IFS# '
          ;;

esac
export PS1

The beforementioned "\h", "\w" etc., are AFAIK specialities of the bash and will not work in ksh.

I hope this helps.

bakunin

edit /etc/profile
add an entry:
PS1="`id -un`@`uname -n`:/$PWD>"
export PS1

your new prompt should look like this if you are logged in as root, your hostname is abcdhost1 and you are in /etc directory:
root@abcdhost1:/etc>