1819803 Members
2987 Online
109607 Solutions
New Discussion юеВ

PS1 for C Shell

 
SOLVED
Go to solution
Arunvijai_4
Honored Contributor

PS1 for C Shell

Hi All,

What is the equivalent for
export PS1="`tput bold``uname -n``tput rmso` \$PWD > "

in C Shell ? I want to set this same variable for C Shell. I use this in /etc/profile (or)and .profile.

Thanks,
Arun
"A ship in the harbor is safe, but that is not what ships are built for"
16 REPLIES 16
Steven E. Protter
Exalted Contributor

Re: PS1 for C Shell

Shalom Arun,

Here is a cut and paste. I belive cshell uses different file names, not .profile.

Its been a while.

open snip....



University Computing Services

A brief guide to using UNIX on UCS computers at Florida State University.

If you need additional help, contact the User Services Helpdesk.
UNIX shells

A Unix shell is a program that interprets user-issued commands and passes them to the operating system kernel for execution. Unix provides three shells, the Bourne shell, the Korn shell, and the C shell. Unix utilities are available under each type of shell. Shell commands and capabilities differ, but each type of shell provides features such as parameter processing, variable definition, and loop structures.

The Bourne shell (sh) is named for its author and is distributed with AT&T Unix system software. The Korn shell (ksh, also known as the K shell) is also named after its author; it is similar to the Bourne shell, but has many extended features. The C shell (csh) was developed at the University of California at Berkeley and has a syntax similar to that of the C programming language; it also has many extended features, but it is not compatible with the Bourne shell. All three shells have lengthy on-line manual pages that describe details that are not included here. For example, the manual pages for the Korn shell can be requested with

% man ksh

In this manual, the character % is shown as the interactive prompt from the C shell, and $ as the prompt from the Korn shell or Bourne shell. The C shell prompt is normally shown in examples, unless the topic is specific to another shell.

Changing the Shell

When you log in to Unix, a shell process will start up for interactive input. You have a choice of working under any of the three command interpreters.

Your login shell type is stored in the system password file. The default for new users is the Bourne shell, sh, but it can be changed with the chsh (change shell) command. chsh does not affect your current session, but takes effect the next time you log in. If you want subsequent logins to be under a C shell, Korn shell, or Bourne shell, the commands are, respectively,

$ chsh loginname /bin/csh
% chsh loginname /bin/ksh
% chsh loginname /bin/sh

where loginname is the user name you are logged in under.

Regardless of which shell you are working under, you can invoke new shell processes with the command csh, ksh, or sh.

Recalling Commands

Korn Shell Command History

The Korn shell allows the user to edit and reissue previous commands, using a special mode similar to a one-line vi editor. The terminal must be set up to run the editor, and you must also set the value of the EDITOR variable in .profile to /usr/bin/vi . The vi editor is described in Appendix A.

At the Korn shell prompt you can press the ESCAPE key to enter command editing mode. The 'k' key moves you "up" through previous commands, and the 'j' key moves you back "down" to more recent commands. You can insert and change the text of the command with a subset of the vi commands. When you are finished editing the command, yyyyyyyyyyyyyexecutes it.

C Shell Command History

The C shell history mechanism keeps track of shell commands issued and allows you to list and reissue previous commands. To display the command history list, enter

% history n

The parameter n is optional and specifies the number of commands to be displayed.

You can display your history list in reverse order, starting at the most recent command, by typing

% history -r

A command can be recalled by number with

% !n

where n is the event number (or use !-n for the nth comand from the most recent). To reissue the last command, type

% !!

You can also specify commands by name. For example,

% !cf77

will issue the most recent command that began with cf77.

You can modify a recent command before re-executing it. For a full explanation, see the History Substitution chapter of the man page for csh.

Tailoring the Environment

Unix shells allow users to tailor their working environments in many ways. Commands can be placed in scripts that are executed automatically at log in for purposes such as defining command aliases and environment variables and execution of certain programs. See Chapter 4 for more information about shell scripts.

The Bourne Shell and Korn Shell .profile Files

The general environment for the Bourne shell and Korn shell is established by the system administrator in the file /etc/profile, which is executed whenever a user logs in under these shells.

A user's private profile can be placed in a file named .profile in his home directory.

Both the Bourne and Korn shells allow the user to define shell functions (see examples below). The string $* indicates where the function parameters should be placed. The Korn shell also permits command aliases, but the Bourne shell does not. Comments can be preceded with the # character.

Environment changes must be exported with the export command in order to affect subshells.

Sample .profile File

# This file is similar to the default .profile file on
# the Y-MP, which users are free to modify. This file is
# read upon the first invocation of sh or ksh.
#
# Some shell functions that define new versions of ls:
#
ls() { /bin/ls -C $*; }
lf() { /bin/ls -CF $*; }
dir() { echo " ";pwd;echo " ";lf;echo " "; }
#
# Aliases work only under Korn shell
#
alias up="cd .."
alias ty='cat'
alias bye='exit'
#
# Default search path
#
PATH=.:$HOME/bin:/bin:/usr/bin:/usr/ucb:/usr/local/bin
export PATH
#
# Define other environment variables
#
EDITOR=/usr/bin/vi; export EDITOR
#
# Set prompt to ymp$:
#
PS1="`uname -n`$ "; export PS1
#
# Set default file permission to exclude other:rw and group:w
#
umask 026


Every user is provided a default .profile file, but is free to modify it to suit his or her preferences. However, users should be careful about modifying the profile, since unexpected things can occur if the wrong commands are inserted.

Under the Bourne or Korn shell you can implement commands in .profile so that they affect the current environment with

$ . .profile

C Shell .login and .cshrc Files

The general environment for the C shell is established by the system administrator in the file /etc/cshrc, which is executed whenever a user logs in under the C shell. Users can modify their own environments by placing commands in scripts named .cshrc and .login in their home directories. The file .logout in the home directory can be created to store commands to be executed each time the user logs out.

The file .cshrc is executed each time a new C shell process is created. It affects any child processes of the user's login process. The .cshrc file can contain variable definitions, aliases for commands, and other types of C shell commands. The file .login is executed only by the user's login shell process, not by subsequent C shell processes.

Sample .cshrc File

# This file is similar to the default .cshrc file on the
# Y-MP, which users are free to modify. This file is read
# upon every invocation of csh.
#
# Aliases
#
alias ls "/bin/ls -C"
alias lf "/bin/ls -CF"
alias dir 'echo " ";pwd;echo " ";lf;echo " " '
alias up "cd .."
alias ty cat
alias bye exit
#
# Default search path
#
set path=(. ~/bin /bin /usr/bin /usr/ucb /usr/local/bin)
#
# Define other variables
#
set history=24
setenv EDITOR /usr/bin/vi
#
# Set prompt to ymp%:
#
set prompt="`uname -n`% "
#
# Set default file permission to exclude other:rw and group:w
#
umask 026

Every user is provided with default .cshrc and .login files, but is free to modify them. Users should be careful about modifying the files, since unexpected things can occur if the wrong commands are inserted.

Under the C shell you can implement commands in .cshrc so that they affect your current environment with

% source .cshrc

close snip ...

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

SEP, Thanks. I will set this in .cshrc file. But my problem is i want more or less similar to PS1 in Bourne with C Shell. I tried with

# set prompt="`whoami`@`hostname` $cwd ->"

It shows the current working directory only once when i execute it. When i traverse across, it remains the same.

Thanks,
Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Godron
Honored Contributor

Re: PS1 for C Shell

Arun,
adapted from another thread:
alias cd 'cd \!*;set prompt="`echo $LOGNAME`@$cwd >"'

Change the cd command
Peter Godron
Honored Contributor

Re: PS1 for C Shell

Arun,
also
set prompt="$LOGNAME $PWD\>"
according to:
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=603965
"How to set PS1 in csh"
Joseph C. Denman
Honored Contributor

Re: PS1 for C Shell

Arun,

It is almost identical:

set prompt="`tput bold``uname -n``tput rmso` $PWD > "

Put the above command in .login or .cshrc

...jcd...
If I had only read the instructions first??
Joseph C. Denman
Honored Contributor

Re: PS1 for C Shell

Nevermind, the $PWD does not change. I'll keep looking#$%^&*(
If I had only read the instructions first??
Peter Nikitka
Honored Contributor

Re: PS1 for C Shell

Hi,

$PWD - stuff and more goodies - not for the prompt only - work very well in the compatible superset c-shell 'tcsh':

set prompt='%n@%B%m%b:%c '

would be good substitute for your PS1.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

Thanks guys. Peter your method works partially, root@/tmp >. I am not getting hostname here.

I found a lengthy method though,

setenv HOST `uname -n`
alias setprompt 'set prompt="$HOST $cwd # "'
alias cd 'cd \!* && setprompt'
alias pushd 'pushd \!* && setprompt'
alias popd 'popd \!* && setprompt'
setprompt

I am looking for a one-liner.

Thanks,
Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Nikitka
Honored Contributor

Re: PS1 for C Shell

Hi Arun,

the setting of the prompt via
set prompt='%n@%B%m%b:%c '
in TCSH worked for me - perhaps use %M instead of %m.
Nevertheless
set prompt="%n@%B`uname -n`%b:%c >"

should work as well.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

Peter, in C Shell it is not working.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Godron
Honored Contributor

Re: PS1 for C Shell

Arun,
I tried on my machine:
$ echo $0
-sh
$ csh
tpol 22: alias cd 'cd \!*;set prompt="`hostname` $cwd >"'
tpol 23: cd /tmp
tpol /tmp >
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

Peter, It doesn't seem to be working.

myhpux /tmp #csh
# alias cd 'cd \!*;set prompt="`hostname` $cwd >"'
#

"A ship in the harbor is safe, but that is not what ships are built for"
Peter Godron
Honored Contributor
Solution

Re: PS1 for C Shell

Arun,
after the alias command do a cd (cd .. or cd /tmp) and your prompt should change
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

Thanks Peter, Found a better way,

# alias cd 'cd \!*;set prompt="`tput bold``hostname``tput rmso` $cwd >"'

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: PS1 for C Shell

Solution : alias cd 'cd \!*;set prompt="`tput bold``hostname``tput rmso` $cwd >"'
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Nikitka
Honored Contributor

Re: PS1 for C Shell

Hi Arun,

ok, lets go back to the old CSH.
I suggest you put all prompt related stuff in a file ~/.prompt like I've done for a project of us (ignore Clearcase-Stuff).

~/.cshrc:
setenv HOST=`uname -n`
if ($?prompt && -f $HOME/.prompt) then
if ("$prompt" != "") source $HOME/.prompt
alias cd "cd \!*; source $HOME/.prompt"
endif

~/.prompt:
if ( $?prompt ) then
if ( $?CLEARCASE_ROOT ) then
if ($?tcsh) then
set icon=${CLEARCASE_ROOT:t}
else
set icon=`basename $CLEARCASE_ROOT`
endif
set ccview=${icon}:
else set ccview= icon=$HOST
endif

if ( ($TERM == xterm) || ($TERM == dtterm) ) then
# $cwd as a window title:
# echo -n "^[]2;${cwd}^G^[]1;${icon}^G"
echo -n "^[]2;${icon}^G^[]1;${icon}^G"
endif
if ($?tcsh) then
set prompt="${HOST}:${ccview}%~:! "
else
set prompt="${HOST}:${ccview}${cwd}:! "
endif
endif

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"