Notes on the bash shell for Unix and Windows 95/98/NT

Following are some notes on the basic commands you may have to use in a Unix environment (for example on Aristotle) or under the Cygwin toolkit, which implements a standard bash Unix shell under Windows 95/98/NT. Since Aristotle uses a C shell we will give the syntax for both flavors of shells. Note that some commands, such as job control, are not available under Windows.

When you type commands to Unix you are communicating with a program which is referred to as the Shell. There are two main flavors of Shell programs, the Bourne-again shell (bash) which is derived from a shell called bsh and the C shell (csh). At the command line type:

echo $SHELL

To find out which shell you are running. The system will usually respond with:

/bin/csh

...or:

/bin/bash

Other possible values are ksh (Korn shell), bsh (Bourne shell) and tcsh (tcsh shell). One difference between the shells is the command line prompt, which is $ in bash and % in csh by default, but these can be changed and often are. Most environments will also display the current username and perhaps directory. So you may see something like:

[elmer@cce unix]$

[elmer@cce] ~/unix %

At the command line you may start entering commands to the system. Remember, UNIX is case-sensitive and therefore most UNIX commands must be entered in lower case. The basic format of UNIX commands is:

command [-option(s)] [argument(s)]

The Unix filesystem

Files in Unix are organized in a hierarchical structure. The hierarchy starts from the root directory "/" (slash) and progresses downwards. To list files in a directory you type "ls". Try typing "ls /" to see the subdirectories of the root directory on your machine.

To acces one of the major directories under root type "/" followed by the directories own name as in /bin.

Some conventions on Unix systems for subdirectories of root are:

/u or /useruser directory
/binbinary directory
/devdevice directory
/etcmiscellaneous directory (usually system files)
/tmptemporary directory

The main thing to remember about filenames and directory names in Unix is that the operating system is case sensitive. A filename can be any combination of 1-255 characters other than slashes (/), asterisks (/), question marks (?), quotation marks (" ) or ('), square brackets ([) or (]), dollar sign ($) or control characters.

To avoid misinterpretation, the safest characters to use for simple filenames are letters of the alphabet, numbers, periods (.), hyphens (-), and underline (_).

A word of warning: do not start file names with a leading hyphen (-) as it will cause great trouble when you try to refer to it in many cases. Also, if a filename starts with a period (.) it will not appear when you type the default command to list files.

The meaning of "~","." and ".."

As a user on a Unix system you have a home directory, and you can refer to this directory by the shorthand tilde (~). So if there is a file called myfile in your home directory, you can access it from anywhere by typing "~myfile".

When you are in a directory and type "cd .." you are taken in to the directory directly preceeding the current directory. The shortand "." is also used when referring to the current directory, as in "./program".

Simple filesystem commands

ls Sorts and displays the names of all the directories and files that reside in your current directory.
ls -la Sorts and displays all directories and files in current directory, with information on the size of files, access and whether the file is executable or not.
cd directorypath Changes into the directory given by directorypath. If you simply type cd, with no path you go to your home directory.

pwd Lists the full path of the current working directory
mkdir directoryname Makes a directory with the name directoryname, under the current working directory if you do not specify a path, but otherwise according to the path given.

mv oldfilename newfilename Changes the name of file oldfilename to newfilename. Also works for directories. Can be used to move direcotires or files around in the filesystem hiearchy, for example mv oldfilename ../oldfilename would move the file into the directory above the current working directory, but keep the last part of the name intact. Caution: Most systems do not prompt you if you give a destination filename that is the same as the name of an existing file. In this example you would overwrite the contents of any existing file with the name newfilename
cp oldfilename newfilename Makes a copy of oldfilename under the name newfilename. To copy a directory with all it's contents, type cp -R olddirectoryname newdirectoryname. Caution: Any file with the name of the new file or directory will be overwritten by the copy.
rm filename Removes (deletes) the file with name filename. On some systems you will be prompted for confirmation, on others the file will be deleted immediately. Caution: There is no undo command in Unix.
rmdir directoryname Removes a directory, if the directory is empty. If you want to remove all files in the directory first, type rm directoryname/*, but be careful about using the wildcard with any of these delete or remove commands. You could also type rm -rf directoryname which deletes all files under this directory, including subdirectories and their contents and finally deletes the directory.
duSummarizes the disk usage of the files and subdirectories in the current directory. To list diskusage by subdirectory (more compact information) type du -s * or for a grand total of the current directory du -s

Permissions

When you type ls -la you get an expanded list of the files in the current directory, with all the permissions associated with each file or subdirectory:

ls -la
total 501
-rw-r-----   1 user group 108   Oct 15 19:10 file.1
-rwxr-x---   1 user group 6452  Oct 15 17:15 program.1
drwxr-xrw-   1 user group 512   Oct 15 19:13 letters

The first character indicates the type of the file, d for directory, - for file or binary. The remaining nine characters represent three sets of three characters each: the first for owner, second the group and the third for all other users. Each of the three sets has lists whether the user, group or others can (r)ead, (w)rite or e(x)ecute the file. If any of these users categories can't read,write or execute a - appears instead. So the listing above can be broken down as follows:

TypeUserGroupOthersFileExplanation
-rw--r----file.1A file which user and group can read, user can write and others have no access
-rwxr-x---program.1A binary which user and group can read and execute, user can change and others have no access to.
drwxr-xrw-lettersA directory which user and group can read from, user can write to and others can see files in and write to.

Changing permissions

You can make changes to permissions by entering a chmod command. It allows the owner of the file to add to (+) or remove from (-) existing permissions. It also allows the owner to clear existing permission and assign all permission from scratch; this is known as assigning permissions absolutely (=). The chmod command affects any of the three types of access for any of the three categories of Unix users, using one-letter symbols in the following order (left to right):

uUser
gGroup
oOthers (not user or group)
aAll on system
+Add following permission(s)
-Remove following permission(s)
=Set from scratch following permission(s)
rPermission to read
wPermission to write
xPermission to execute

Standard input, output and redirection

The shell and many UNIX commands take their input from standard input (stdin), write output to standard output (stdout), and write error output to standard error (stderr). By default, standard input is connected to the terminal keyboard and standard output and error to the terminal screen.

You can redirect input and output by specifying destination on the command line using a redirection metacharacter followed by the desired destination. Some of the forms in csh and bash are:

CharacterAction
>Redirect standard output to file
<Redirect standard input to program
|Redirect standard output to another command (pipe)
>>Append standard output to file

Sometimes you can combine these on the same line, for example if you want to run an econometrics package, taking commands form file commandfile and sending output to outputfile:

package < commandfile > outfile

The path

When you issue a command, the shell program parses the command line and either processes it directly or searches for a file with that name in any of the directories specified in your search path , which is controlled by the shell variable PATH. If the file is not found in any of the directories in your search path, the shell reports that the command was not found. The file may well be on the disk somewhere, but it is "not in your path."

To figure out which PATH you are using, type echo $PATH. In Unix you will see something like:

/usr/local/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin: /usr/ucb:/usr/X11R6/bin:

This means that the system looks in all these directories for executables. If you typed something in the shell and nothing happened, the problem may be that the program is not on the path. You will either have to provide the full path or change your PATH variable.

Changing variables and modifying the PATH

The PATH is one of the variables in the shell and can be modified, as other variables. The syntax to change a variable in the sh,bsh or bash shell is:

var=value

export var

While the syntax for the csh is:

setenv var value

If you type this in from the console, the change is only in effect as long as you are logged in. To make a permanent change, which takes effect each time that you log in you need to put the variable declaration into a special system file. On a bsh or bash shell, put the variable declaration in the style above into the file .bashrc in your home directory, but on a csh system put the change into the file .cshrc. On bsh or bash systems you can also put these declarations in the .profile file and on csh system into the .login file.

To check if a variable is set to a certain value, type echo $VAR, note the dollar ($) sign in front of the variable name.

If you edit one of the files named above, or any other file containing variable declarations for that matter, and want to have the changes take effect without login out and in again, simply type:

source filename

Some bsh systems may not understand this, in which case you should simply type .filename to "execute" it.

The alias

You can also change the name of a command, or associate a name with a series of commands, through an "alias". For a one time change, which is only in effect while you are logged in, on bsh or bash systems type:

alias x=y

Where x is the name of the new command and y is the old command, and on csh systems type:

alias x y

For a more permanent change, put these declarations into the files named above, which are read by the system every time you log in or out. To remove an alias, type unalias command, where command is the alias you have defined. For example, on Nicco the rm (remove file) command is an alias for rm -i, which prompts you for a confirmation each time you want to delete a file. To remove this safety feature, simply type unalias rm - but be careful about using it.

Job control (not available in Windows)

Unix allows you to execute several programs in the same shell, by putting programs in the "background". To execute a command in the background, type:

command &

You can also switch a command from background to forground by typing fg and to background again by typing bg.

You can list all the jobs that you are currently running, by typing ps, and jobs for all users on the system by typing ps -aux. Most Unix systems also have a program called top which displays a screenful of dynamically updated information, that shows the load on the processor and the most resource intensive jobs, with the amount of memory and CPU time that they are occupying. This can be very valuable to understand how your programs behave. Type q to break out of top.

Finally, if a program locks up or misbehaves you may have to terminate execution. To do this you use the kill command. It can actually send various signals to a process, such as to restart a part of the OS, but you will most likely be using kill -9 jobID where jobID is a number that you have obtained from the ps command or by running top. You can also perform a kill from top, simply type k in the console and fill in the jobID when you are promted for it.

Several useful commands

cat, more, less and tail

Of these four commands only cat is implemented under Windows.

If you want to "type" a file and send it as input to some process, use the cat command. The following effectively copies the file oldfile to newfile:

cat oldfile > newfile

The command cat is one of many that use stdout as a default, so if you type simply cat filename you list the whole file on screen. The file will simply scroll by on the screen, but you can stop that by piping the output of cat to another program called more which acts as a buffer to hold the text. This command is not available in Windows. The command to show one screenfull at a time of the file filename is:

cat filename | more

Hitting the spacebar moves you down one line in the buffer, and return gives you the next screenful.

Some systems also allow you to type more filename which has the same effect as the command above. And most Unix systems today also have a less command, that adds a nice feature to more, which is that you can navigate backwards as well as forwards in the buffer.

Another useful command, which prints the last few lines of a textfile is tail. This command is also not available in Windows.

grep

This command is very powerful, and it would take a long time to explain the full range of options, but basically it is useful if you want to look for a string in text, either from a file or from the output of another command. For example to list all the files in a directory which contain "fudd" in their name, you type:

ls -la |grep fudd

Where the output of ls is now "piped" into grep. To search for text in a particular file, type:

grep fudd filename

Another useful feature is that you can ask grep to return all text that does not have a particular string in it, so for example to list the set that is the complement of the files in the list command above, type:

ls -la |grep -v fudd


Benedikt Stefansson <benedikt@ucla.edu>