[ Team LiB ] Previous Section Next Section

Tasks Common to All Shells

The following sections describe concepts and tasks that can be used with any shell.

Setting a Default Shell

graphics/new.gif

The user's login shell is set in the last field of the user's entry in the Passwd database or /etc/passwd file. Use the SMC System Configuration:Users: User Accounts tool to edit the Passwd database in a networked environment.

Changing Shells from a Command Line (csh, ksh, sh, bash, tcsh)

If you want to use another shell without modifying the Passwd database, you can change shells at a command-line prompt by simply typing the name of the shell you want to use.

To change to the C shell, type csh and press Return. The default C shell prompt is the system name followed by a percent sign (%).


$ csh
paperbark%

To change to the Korn shell, type ksh and press Return. The default Korn shell prompt is a dollar sign ($).


paperbark% ksh
$

To change to the Bourne shell, type sh and press Return. The default Bourne shell prompt also is a dollar sign ($).


$ sh
$

graphics/new.gif

To change to the Bourne-Again shell, type bash and press Return. The default Bourne-Again shell prompt is bash- version$, where version is the particular bash shell you are using.


paperbark% bash
bash-2.03$

graphics/new.gif

NOTE. The default prompt can change if you apply subsequent Solaris patches to the shell.


To change to tcsh, type tcsh and press Return. The default tcsh prompt is >.


paperbark% tcsh
>

To change to the Z shell, type zsh and press Return. With no startup files, the prompt does not change.

Quitting from a Shell (exit)

If you start a new shell from the command line, you can quit it and return to the old shell. To quit from a shell, type exit and press Return. If you have started (layered) another shell, you are returned to the original shell prompt.


$ exit
oak%



Setting Aliases, Paths, and Environment Variables

graphics/new.gif

Environment variables in a shell are inherited by all subsequent shells and any programs you start from them. For shells that have two login files—all but the Bourne shell—you should set all environment variables in the .login file.

Aliases and functions are not inherited by subsequent shells. They are redefined for each shell separately. Set aliases and functions in the .cshrc file for the C shell, in the . ksh_env file for the Korn shell, and in the relevant files for tcsh, bash, and zsh shells.

The following example shows what can go wrong if you don't follow the guidelines about where to set environment variables. Using the C shell as an example, suppose you know your login C shell sources $HOME/.cshrc whenever you start a shell—call this Shell1. Now, you add the following command to the PATH variable in $HOME/.cshrc.


setenv PATH ${PATH}:/my/dir

If you now start another shell from Shell1, that shell inherits the PATH setting from Shell1 (because it's an environment setting), but your $HOME/.cshrc file gets sourced again, thus adding : ...:/my/dir:/my/dir .to the end of your PATH. Not good. This construct can, over time, overflow your PATH environment variable, resulting in an administrative nightmare.

However, when you use $HOME/.login as the place to set the PATH environment variable, your login shell gets the definition of PATH and all subsequent shells inherit it.

NOTE. You must export environment variables for the Bourne, Korn, Bourne-Again, and Z shells so that their children can inherit them.


The Solaris Operating Environment provides several default environment variables.

  • PS1 defines the shell prompt for the Bourne and Korn shells. The default prompt for the Bourne and Korn shells is $. The default prompt for root in either shell is #.

  • HOME defines the absolute path to the user's home directory. The default value for HOME is automatically defined and set to the login directory specified in the /etc/passwd file as part of the login process. The shell subsequently uses this information to determine the directory to change to when you type the cd command without an argument.

  • LOGNAME defines the user's login name. The default value for LOGNAME is automatically defined and set to the login name specified in the /etc/passwd file as part of the login process.

  • PATH lists, in order, the directories that the shell searches to find the program to run. When the user executes a command and uses the full path name, the shell finds the command by using that path name. However, when the user specifies only a command name, the shell searches the directories for the command in the order specified by the PATH variable. If the command is found in one of the directories, the shell executes it. If the directory containing the command is not in the search path, the user must then type the complete path name of a command.

    The default PATH is automatically defined and set as specified in .profile (Bourne or Korn shell) or .login (C shell) as part of the login process (see "Setting the Path for Bourne and Korn Shells" and "Setting the Path for Bourne and Korn Shells" on page 119 for details). When identically named commands exist in different locations, the first command found with that name is used. For example, suppose that PATH is defined (in Bourne and Korn shell syntax) as PATH=/bin:/usr/bin:/usr/sbin:$HOME/bin; then suppose a file named sample resides in both /usr/bin and /home/jean/bin. If the user types the command sample without specifying its full path name, the version found in /usr/bin is used.

    A default su path (/sbin:/usr/sbin:/usr/bin:/etc) is set by the system, but most users modify it to add other command directories. Many user problems related to setting up the environment and accessing the right version of a command or a tool can be traced to incorrectly defined paths.

CAUTION. Including . in the path to search the current directory is a potential security problem. If security is an issue at your site, do not include . as part of a user's path. Never use . as part of the root path.


  • The LANG and LC environment variables specify the locale-specific conversions and conventions for the shell, such as time zones, collation order, and format of dates, time, currency, and numbers. In addition, you can use the stty command in a user-initialization file to set whether the system supports multibyte characters.

    LANG sets all possible conversions and conventions for the given locale. If you have special needs, you can set various aspects of localization separately by using the following LC variables.

    • LC_COLLATE

    • LC_CTYPE

    • LC_MESSAGES

    • LC_NUMERIC

    • LC_MONETARY

    • LC_TIME

    Table 28 lists the values for the LANG and LC environment variables.

Table 28. Values for LANG and LC Variables

Value

Locale

DE

German.

FR

French.

ISO_8859_1

English and European.

IT

Italian.

JAPANESE

Japanese.

KOREAN

Korean.

SV

Swedish.

TCHINESE

Taiwanese.

Other environment variables include the following.

  • ARCH sets the user's system architecture (for example sun4, i386). You can set this variable in the Bourne or Korn shells with ARCH ='uname -p' or in the C shell with setenv ARCH 'uname -p'. No built-in behavior of the shell depends on this variable. However, it is a useful variable for branching within shell scripts.

  • CALENDAR sets the path to the Calendar executables.

  • CDPATH (or cdpath in the C shell) sets a variable used by the cd command. If the target directory of the cd command is specified as a relative path name, the cd command first looks for the target directory in the current directory (.). If the target is not found, the path names listed in the CDPATH variable are searched consecutively until the target directory is found and the directory change is completed.

  • DESKSET sets the path to the DeskSet executables.

  • history sets history for the C shell.

  • LD_LIBRARY_PATH sets the search path for dynamically linked libraries.

  • LPDEST sets the user's default printer.

  • MAIL tells the shell where to look for new mail.

  • MANPATH sets the hierarchies of the available manual pages.

  • MANSECTS sets the available sections of manual pages.

  • OPENWINHOME sets the path to the OpenWindows executables.

  • prompt defines the shell prompt for the C shell.

  • SHELL sets the default shell used by make, vi, and other tools.

  • TERM sets the terminal definition. This variable should be reset in /etc/profile or /etc/.login. When the user invokes an editor, the system looks for a file with the same name as the definition of this environment variable. The system searches the directory referenced by TERMINFO to determine the terminal characteristics.

  • TERMINFO specifies the path name for an unsupported terminal that has been added to the terminfo file. Use the TERMINFO variable in /etc/profile or /etc/.login.

  • TZ sets the time zone.

Users and system administrators can define additional variables for their own use. When you define an environment variable from a shell command, the variable remains in effect while you are working in the shell. When you exit the shell, the environment variable is not retained. Store "permanent" environment variables that are likely to be used during each login session in the .profile or .login file. The syntax for defining environment variables depends on the shell and is described elsewhere in this chapter.

Setting the Path for Bourne and Korn Shells

The path for the Bourne and Korn shells is specified in the user's $HOME/.profile file as shown in the following example.


PATH=/usr/bin:/$HOME/bin:.



Setting the Path for the C Shell

The path for the C shell is specified in the user's $HOME/.cshrc file (with the set path environment variable) as shown in the following example.


set path = (/usr/bin $home/bin .)

See the appropriate manual pages for an in-depth description of these commands, and also see Chapter 12, "Recognizing File Access Problems," for more information about troubleshooting problems with paths.

Displaying Environment Variable Settings (env)

Each shell maintains an environment with a set of specifications that it gets from the user's initialization files (.profile for the Bourne and Korn shells or .cshrc and .login for the C shell) or from environment variables set interactively from a shell. These environment variables can specify information such as the user's home directory, login name, default printer, location for e-mail messages, and path for accessing the OpenWindows environment.

To display a list of the current environment variable settings, type env and press Return.

The following example shows all the environment variables for a system running CDE.


paperbark% env
MANPATH=/usr/dt/man:/usr/man:/usr/openwin/share/man
DTDATABASESEARCHPATH=/export/home/winsor/.dt/types,/etc/dt/appconfig/types/%L,
  /etc/dt/appconfig/types/C,/usr/dt/appconfig/types/%L,/usr/dt/appconfig/types
  /C
DTXSERVERLOCATION=local
LANG=C
HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help
DTSOURCEPROFILE=true
PATH=/usr/openwin/bin:/usr/dt/bin:/export/home/opt/SUNWadm/bin:/bin:/usr/bin:/
 usr/sbin:/usr/ucb:/etc:/usr/proc/bin:/usr/ccs/bin:/opt/hpnp/bin:/opt/NSCPcom
 :/usr/local/games:.
AB_CARDCATALOG=/usr/dt/share/answerbooks/C/ab_cardcatalog
DTUSERSESSION=winsor-paperbark-0
XMICONBMSEARCHPATH=/export/home/winsor/.dt/icons/%B%M.bm:/export/home/winsor/.
  dt/icons/%B%M.pm:/export/home/winsor/.dt/icons/%B:/etc/dt/appconfig/icons/%L
  /%B%M.bm:/etc/dt/appconfig/icons/%L/%B%M.pm:/etc/dt/appconfig/icons/%L/%B:/e
  tc/dt/appconfig/icons/C/%B%M.bm:/etc/dt/appconfig/icons/C/%B%M.pm:/etc/dt/ap
  pconfig/icons/C/%B:/usr/dt/appconfig/icons/%L/%B%M.bm:/usr/dt/appconfig/icon
  s/%L/%B%M.pm:/usr/dt/appconfig/icons/%L/%B:/usr/dt/appconfig/icons/C/%B%M.bm
  :/usr/dt/appconfig/icons/C/%B%M.pm:/usr/dt/appconfig/icons/C/%B
SESSION_SVR=paperbark
OPENWINHOME=/usr/openwin
EDITOR=/usr/dt/bin/dtpad
LOGNAME=winsor
DTSCREENSAVERLIST=StartDtscreenSwarm  StartDtscreenQix      StartDtscreenFlame
  StartDtscreenHop StartDtscreenImage StartDtscreenLife     StartDtscreenRotor
  StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank
MAIL=/var/mail/winsor
USER=winsor
DISPLAY=:0.0
SHELL=/bin/csh
DTAPPSEARCHPATH=/export/home/winsor/.dt/appmanager:/etc/dt/appconfig/appmanage
  r/%L:/etc/dt/appconfig/appmanager/C:/usr/dt/appconfig/appmanager/%L:/usr/dt/
  appconfig/appmanager/C
HOME=/export/home/winsor
XFILESEARCHPATH=/usr/openwin/lib/locale/%L/%T/%N%S:/usr/openwin/lib/%T/%N%S
XMICONSEARCHPATH=/export/home/winsor/.dt/icons/%B%M.pm:/export/home/winsor/.dt
  /icons/%B%M.bm:/export/home/winsor/.dt/icons/%B:/etc/dt/appconfig/icons/%L/%
  B%M.pm:/etc/dt/appconfig/icons/%L/%B%M.bm:/etc/dt/appconfig/icons/%L/%B:/etc
  /dt/appconfig/icons/C/%B%M.pm:/etc/dt/appconfig/icons/C/%B%M.bm:/etc/dt/appc
  onfig/icons/C/%B:/usr/dt/appconfig/icons/%L/%B%M.pm:/usr/dt/appconfig/icons/
  %L/%B%M.bm:/usr/dt/appconfig/icons/%L/%B:/usr/dt/appconfig/icons/C/%B%M.pm:/
  usr/dt/appconfig/icons/C/%B%M.bm:/usr/dt/appconfig/icons/C/%B
TERM=dtterm
dtstart_sessionlogfile=/dev/null
TZ=Australia/West
DTHELPSEARCHPATH=/export/home/winsor/.dt/help/winsor-paperbark-0/%H:/export/ho
 me/winsor/.dt/help/winsor-paperbark-0/%H.sdl:/export/home/winsor/.dt/help/wi
 nsor-paperbark-0/%H.hv:/export/home/winsor/.dt/help/%H:/export/home/winsor/.
 dt/help/%H.sdl:/export/home/winsor/.dt/help/%H.hv:/etc/dt/appconfig/help/%L/
 %H:/etc/dt/appconfig/help/%L/%H.sdl:/etc/dt/appconfig/help/%L/%H.hv:/usr/dt/
 appconfig/help/%L/%H:/usr/dt/appconfig/help/%L/%H.sdl:/usr/dt/appconfig/help
 /%L/%H.hv:/usr/dt/appconfig/help/C/%H:/usr/dt/appconfig/help/C/%H.sdl:/usr/d
 t/appconfig/help/C/%H.hv
XMBINDDIR=/usr/dt/lib/bindings
WINDOWID=96469001
TERMINAL_EMULATOR=dtterm
PWD=/export/home/winsor
paperbark%




Clearing a Shell Window (clear)

You can clear the contents of a shell window and redisplay the prompt at the top of the window. To clear the contents of a shell window, type clear and press Return.


oak% ls -l /home/ignatz/quest
-rw-r--r--   1 ignatz   staff              24 Jul 16 15:07 quest
oak% clear

The window is cleared and the prompt is redisplayed at the top.

Setting the Shell Backspace Key (stty erase)

If you want to change the erase key from Delete to Backspace, type stty erase, then press Control and Shift together, and then type H and press Return. The Backspace key is set as the erase key. The following example changes the erase key for the C shell.


oak% stty erase ^H
oak%



Typing Several Commands on the Same Command Line (;)

You can type more than one command on a single command line by typing a semicolon (;) between the commands.

For example, you can change to a directory and list the commands by typing cd /usr/bin;ls and pressing Return. The following example sets an environment variable for the Bourne shell and then exports the variable.


$ PATH=/usr/bin:$HOME/bin:.;export PATH
$



Redirecting Output (<>)

Unless you indicate otherwise, commands normally display their results on the screen. You can, however, redirect the output of a command by using the redirect symbols < and >. For example, to save the output to a file instead of displaying it on the screen, use the > redirect symbol to tell the shell to put the contents into a file. In the following example, the output of the date command is redirected to a new file called date.file.


$ date > date.file
$

The following example shows the contents of date.file.


paperbark% more date.file
Wed May  3 15:59:50 WST 2000
paperbark%

You can also redirect input in the other direction. For example, to mail the contents of a file to user ignatz@oak, type mail ignatz@oak < report.file and press Return. The file called report.file is sent by e-mail to ignatz@oak.

Combining Commands (|)

You can use the pipe (|) operator to connect two or more commands, using the output from one command as the input to the next one. This section provides two examples of the many ways you can combine commands in a pipeline.

To print the cat(1) manual page, type man cat | lp and press Return. The manual page is not displayed on the screen. Instead, the output is sent to the lp command, which prints it on the default printer.

You can search the process list for a particular command by piping the output of ps -e to the grep command. The output is displayed on the screen. The following example displays process information for OpenWindows.


cinderella% ps -e | grep openwin
   260 ?        0:00 openwin
cinderella%

If you want to print the information, you can add an additional pipe command (| lp) to the end of the sequence and send it to the printer, as shown in the following example.


anastasia% ps -e | grep openwin | lp
request id is castle-51 (request id is castle-51 (standard input)
anastasia%



    [ Team LiB ] Previous Section Next Section