[ 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


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

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


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


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

Setting Aliases, Paths, and Environment Variables


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_CTYPE




    • LC_TIME

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

Table 28. Values for LANG and LC Variables








English and European.











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.


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
DTSCREENSAVERLIST=StartDtscreenSwarm  StartDtscreenQix      StartDtscreenFlame
  StartDtscreenHop StartDtscreenImage StartDtscreenLife     StartDtscreenRotor
  StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank

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

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

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

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)

    [ Team LiB ] Previous Section Next Section