McGill University School of Computer Science
McGill University School of Computer Science
Views
You are here: Unix FAQ

Unix FAQ

From Socs Info Wiki

Contents

What is a shell ?

A shell is a program that interprets your command lines and runs other programs. Another name for the shell is "command interpreter". A shell can read command lines from a terminal ( interactively ) or it can read them from a file. Either way, upon reading a command, it then executes it. When shell commands reside in a file, this is refered to as a shell script, or shell program. There are programming constructs that make it possible to write shell programs that are much more powerful than just a list of commands.

What types of shells are there ?

There are dozens of shells available; however certain shells have become most common in UNIX. Namely, Bourne shell (sh), Berkeley UNIX C shell (csh), Korn shell (ksh), tcsh, and GNU Bourne-Again SHell (bash). Tcsh is an enhanced and completely compatible version of csh, while bash is an enhanced and compatible version of sh. Bash also incoporates useful features from ksh and csh.

What are some of the quirks of these shells ?

Each shell has it's own quirks, listed here:

  • sh - The Bourne shell is the oldest of the current UNIX shells, thus it is a bit primitive and lacks job control features. Despite this, most UNIX users consider the Bourne shell superior for shell programming and writing command files.
  • csh - The Berkeley UNIX C shell has been by far the most popular shell for interactive use. It has a lot of nice features that aren't avialable in the Bourne shell, including filename completion, command aliasing, history substitution, job control and a number of built-in commands. Csh will read shell scripts and has some useful features for programmers. While you won't have trouble with normal usage, it unfortunately has some problems that make shell programming tough.
  • tcsh - Tcsh has the following features not available in csh. A command-line editor, which supports GNU Emacs or vi-style key bindings. Programmable, interactive word completion and listing. Spelling correction of filenames, commands and variables. Editor commands which perform other useful functions in the middle of typed commands, including documentation lookup, quick editor restarting, and command resolution. An enhanced history mechanism. Enhanced directory parsing and directory stack handling. File inquiry operators. Automatic, periodic and timed events. Support for the Native Language System. Extensive terminal-management capabilities. New variables, including read-only variables.
  • ksh - The Korn shell is a superset of the sh shell language, but has most of the C shell's features plus some completely new freatures like history editing. It is also more reliable than csh. It is a standard on UNIX SVR4.
  • bash - GNU Bourne-Again SHell is an sh-compatible shell with features from ksh and csh. It was developed by the Free Software Foundation. Bash includes interactive command-line editing, job control on architectures that support it, csh-like history features and brace expansion.

What files do these shells use ?

Each shell uses different files, listed here:

sh Files:

  • /etc/profile System-wide environment settings. Allows the system administrator to perform services for the entire user community. Typical services include: the announcement of system news, user mail, and the setting of default environment variables.
  • ~/.profile User-specific environment. Settings include variables and terminal modes.
  • /dev/null
  • /tmp/sh*Temporary file for `<<'.

When the shell is invoked, commands are initially read from /etc/profile and from $HOME/.profile, if such files exist.

csh Files:

  • ~/.cshrc Read at beginning of execution by each shell.
  • ~/.login Read by login shells after .cshrc at login.
  • ~/.logout Read by login shells at logout.
  • ~/.history Saved history for use at next login.
  • /tmp/sh* Temporary file for `<<'.
  • /etc/passwd Source of home directories for `~name'.

When first started, the C shell normally performs commands from the .cshrc file in your home directory. Next the .login file in your home directory.Typically, the .login file contains commands to specify the terminal type and environment.

ksh Files:

  • /etc/profile System-wide environment settings.
  • ~/.profile User-specific environment.
  • /etc/suid_profile

tcsh Files:

  • /etc/csh.cshrc Read first by every shell. ConvexOS, Stellix and Intel use /etc/cshrc and NeXTs use /etc/cshrc.std. A/UX, AMIX, Cray and IRIX have no equivalent in csh(1), but read this file in tcsh anyway. Solaris 2.x does not have it either, but tcsh reads /etc/.cshrc. (+)
  • /etc/csh.login Read by login shells after /etc/csh.cshrc. ConvexOS, Stellix and Intel use /etc/login, NeXTs use /etc/login.std, Solaris 2.x uses /etc/.login and A/UX, AMIX, Cray and IRIX use /etc/cshrc.
  • ~/.tcshrc (+) Read by every shell after /etc/csh.cshrc or its equivalent.
  • ~/.cshrc Read by every shell, if ~/.tcshrc doesn't exist, after /etc/csh.cshrc or its equivalent. This manual uses "~/.tcshrc" to mean "~/.tcshrc or, if ~/.tcshrc is not found, ~/.cshrc".
  • ~/.history Read by login shells after ~/.tcshrc if savehist is set, but see also histfile.
  • ~/.login Read by login shells after ~/.tcshrc or ~/.history. The shell may be compiled to read *~/.login before instead of after ~/.tcshrc and ~/.history; see the version shell variable.
  • ~/.cshdirs (+) Read by login shells after ~/.login if savedirs is set, but see also dirsfile.
  • /etc/csh.logout Read by login shells at logout. ConvexOS, Stellix and Intel use /etc/logout and NeXTs use /etc/logout.std. A/UX, AMIX, Cray and IRIX have no equivalent in csh(1), but read this file in tcsh anyway. Solaris 2.x does not have it either, but tcsh reads /etc/.cshrc. (+)
  • ~/.logout Read by login shells at logout after /etc/csh.logout or its equivalent.
  • /bin/sh Used to interpret shell scripts not starting with a `#'.
  • /tmp/sh* Temporary file for `<<'.
  • /etc/passwd Source of home directories for `~name' substitutions.

The tcsh login shell begins by executing commands from the system files /etc/csh.cshrc and /etc/csh.login. It then executes commands from files in the user's home directory: first ~/.tcshrc (+) or, if ~/.tcshrc is not found, ~/.cshrc, then ~/.history (or the value of the histfile shell variable), then ~/.login, and finally ~/.cshdirs (or the value of the dirsfile shell variable) (+). The shell may read /etc/csh.login before instead of after /etc/csh.cshrc, and ~/.login before instead of after ~/.tcshrc or ~/.cshrc and ~/.history, if so compiled; see the version shell variable. (+)

bash Files:

  • /bin/bash The bash executable.
  • /etc/profile System-wide environment settings.
  • ~/.bash_profile The personal initialization file, executed for login shells.
  • ~/.bashrc The individual per-interactive-shell startup file.
  • ~/.inputrc Individual readline initialization file.

Which shell(s) should I use ?

It is partially a matter of preference, and largely a matter of what you want to do. For instance, if you are looking for a command line interpreter that has many useful features, you may want to use tcsh. If you want to do shell programming, you may to use sh. It is common to use multiple shells: one for command-line input, and another for shell scripting.

How can I change shells temporarily / permanently ?

To change shells temporarily, you simply type the name of the shell and press ENTER. This will execute the specified shell until you exit it. To change shells permanently, you must set the SHELL environment variable to the absolute pathname of the login shell you wish to use by default. In order to set the SHELL environment variable, enter the following:

% chsh

How can I change environment variables ?

The answer is shell dependent. Let's say that we want to add the path /usr/ucb (where vi is commonly found) to the environment variable PATH:

tcsh% setenv PATH $PATH:/usr/ucb
csh% setenv PATH $PATH:/usr/ucb
bash% export PATH=$PATH:/usr/ucb
sh% PATH=$PATH:/usr/ucb
  % export PATH

What is the difference between "set" and "setenv" ?

The command "set" sets shell variables, whereas the command "setenv" sets environment variables.

A typical tcsh "set" command is 'set history=25'. This would save the last 25 commands on the history list. Another similar command could be 'set history=25 savehist=20' which does the same as the previous command as well as saving the last 20 commands between logins.

A typical tcsh "setenv" command is 'setenv TERM=xterm'. This would set the environment variable TERM to xterm, thus telling the shell how to handle the terminal.

Thus "set" sets variables specific to the shell, whereas "setenv" sets environment (global) variables usable by any programs.

What environment and shell variables does my shell support ?

Do a "man" on you shell, and search for the words ENVIRONMENT and/or VARIABLE, as these words are usually the subparts of man page section titles.

How can I make certain variables be automatically set upon login ?

This depends on which shell you are using. For instance, if your shell is csh, or tcsh, you would include the variable setting lines in your .cshrc file. If you are using sh, or ksh, then the appropriate file to modify would be ~/.profile whereas with bash, you would make the inclusion in ~/.bashrc

How can I log onto machines or servers ?

There is a range of ways to do this. There are several commands in the range of insecure (commnunication performed in the clear) and secure (communication with the remote command or shell is automatically encrypted). telnet, rsh, and rlogin are examples of insecure remote login programs, and should not be used. ssh is a secure shell client.

What is ssh ?

Secure SHell client, is a remote login program, that enables logging into a remote machine for executing commands in a remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.

SYNOPSIS:  ssh [-l login_name] hostname [command]

Ssh connects and logs into the specified host (hostname). The user must provide her identity to the remote machine using one of several methods.

What is the difference between ssh and slogin ?

Nothing. slogin is a link to ssh. This was done simply because people were accustomed to using rlogin.

What happens when I log in ?

Whenever you start a new shell with a program such as ssh, rlogin or telnet, the first thing that happens is a login program does some administrative duties and then starts your shell.

How can I disconnect/suspend ssh ?

~. to disconnect. ~^Z to suspend

If I set my file creation mask (umask) in ~/.login, will the mask be the same when I start another shell ?

When you start another shell using rlogin or telnet, your umask will be the same whether you put the umask command ~/.login or ~/.cshrc, however the best place to set your umask is in ~/.cshrc, as some programs like rsh source ~/.cshrc when carrying out commands on remote systems. Make sure to place the umask command before the non-interactive-shell check.

If I run a non-interactive shell, will the entire .cshrc file get read ?

Depends on whether you are using the default SOCS .cshrc file ( /usr/socs/Cshrc ). The aforementioned file contains the lines:

#
#  Skip remaining setup if not an interactive shell.
#
if ($?USER == 0 || $?prompt == 0) exit

The "if" condition checks whether this is an interactive session or not. If it is not interactive, "exit" is invoked, and the rest of the file is not read. You may add this line to ~/.cshrc if you know where and why.

When one ftp's a file, what permissions does that file get upon arrival ?

If you ftp a file from a remote host to your account (ie. using "get" or "mget"), the file will be given the permissions as set by your umask. When you send a file from your account to a remote host (ie. using "put" or "mput"), the file will be given the "rw" permissions for user, group, and others on that remote machine.

How can I lookup information about a particular system user ?

Use the finger program.

% finger username

How can I modify the plan, and project fields of my account information ?

In your home directory, create files called .plan, and .project. Whatever is contained within these files will appear in its respective field when a user fingers you. Make these files world readable, and make sure that your home directory is world executable.

How can I forward my email to another account ?

Contact the SOCS Help Desk.

How do I change my password ?

Use the command "passwd" on 'mimi.cs.mcgill.ca'.

% passwd

Note that you have to wait a while for the change to take effect throughout the system.

How do I change permissions on files and directories ?

Use the "chmod" program.

How do I kill a job ?

To see what jobs you are running do:

% jobs

A more verbose form would include the -l flag.

Some examples:

% jobs
[1]  + Suspendedvi helloWorld.c
% jobs -l
[1]  + 24614 Suspendedvi helloWorld.c

In this case, to do a regular kill you can type either

% kill %1

or

% kill 24614

By the way, the "%" sign between "kill" and "1" is supposed to be typed. To check that the kill was successful, reissue the command. If it does not say something like "kill: No such job.", then reissue the command with a "-9" or "-KILL" flag.

% kill -9 %1

Use of the -9 or -KILL flag should be avoided whenever possible, as this signal kills the process without cleanup.

How do I get rid of the "^M" characters produced when I for instance bring a file from a DOS or Windows machine ?

Use the "dos2unix" program.

% dos2unix helloWorld.c > helloWorld.c.clean

Now helloWorld.c.clean does not have any "^M"'s

How can I see the hidden files in my directory ?

All files that start with a "." are hidden files, and are not visible with a simple "ls". You can view these files by appending the "-a" flag.

% ls -a

Examples of hidden files are .login and .bashrc.

How can I check my disk space quota ?

A personal disk space quota is the amount of disk space allocated to a user. You must check your this quota on a regular basis, as to not exceed the limit. There are two limits, namely the soft and hard limit. When the soft limit is reached, you will be warned; however when the hard limit is reached you will not be able to log into your account. Check your quota by invoking the "quota" command, or "du" for more precise measurement. "quota" will not show immediate changes.

% quota -v
% du -s .

How can I get a summary of UNIX commands related to some keyword ?

Use the "man" utility:

% man -k <keyword>

For example, if you could not remember the command for changing your password, you could execute the command:

% man -k password

The output will be a list of commands, and descriptions containing the keyword "password". Since the list is long, one way to narrow it, would be to pipe the output to the "grep" command. For example you could execute:

% man -k password | grep change

The output is now narrowed to just three commands:

chage (1)            - change user password expiry information
dpasswd (8)          - change dialup password
passwd (1)           - change user password

If you had ran "man -k password" without piping it to "grep change", you would get a fairly long list of commands. It turns out that the command "password" appears twice in this list:

passwd (1)           - change user password
passwd (5)           - The password file

This output means that in section 1 and 5 of the manual pages exist entries which contain the word "password". To access the man page regarding changing a user password, one would have to enter:

% man 1 passwd

In order to access the manual page regarding "The password file", one would type:

% man 5 passwd

In the case that a command appears in more then one section of the manual (as does "passwd", appearing in both section 1 and 5), then simply typing

% man <command>

will display the man page with the first occurence of that command.

What do the bracketed numbers following commands names mean ?

The numbers correspond to sections of the manual. The table below shows the section numbers and types of pages contained within each section.

1   Executable programs or shell commands
2   System calls (functions provided by the kernel)
3   Library calls (functions within system libraries)
4   Special files (usually found in /dev)
5   File formats and conventions eg /etc/passwd
6   Games
7   Macro packages and conventions eg man(7), groff(7).
8   System administration commands (usually only for root)
9   Kernel routines [Non standard]