Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects

Further Shell

How to pass information to a program

In general, how to pass information to a program:

  1. Set environment variables. All programs can read these.

  2. Command-line arguments.
    Program icon - Properties.

  3. Pipe to the program, where the program expects to read from standard input.

  4. Configuration files.
    See   .* files in home directory on UNIX.
    e.g. See:
    • $HOME/.mozilla/firefox/id/prefs.js
    Equivalents exist on Windows. e.g. See:
    • C:\Program Files\Mozilla Firefox\defaults\pref\firefox.js
    Edit - Preferences
    Tools - Options

  5. Talk to the program while it is running. How to do this depends on the OS.

What shells are installed?

DCU Linux default is bash shell, but there are others installed.

See what is installed:

ls -l /bin/*sh
On DCU Linux, something like:

-rwxr-xr-x 1 root root 656584 Oct 10  2014 /bin/bash
lrwxrwxrwx 1 root root      4 Feb 12  2014 /bin/csh -> tcsh
lrwxrwxrwx 1 root root     21 Feb 12  2014 /bin/ksh -> /etc/alternatives/ksh
-rwxr-xr-x 1 root root 146512 Sep 28  2013 /bin/sash
lrwxrwxrwx 1 root root      4 Oct 21  2014 /bin/sh -> bash
-rwxr-xr-x 1 root root 387184 Oct 23  2013 /bin/tcsh
lrwxrwxrwx 1 root root     12 Nov 25  2014 /bin/zsh -> /usr/bin/zsh

To start using one as the command-line, just type its name. e.g.

"exit" to return.

The shebang line

The first line of a shell script should state which shell it is to run in, using syntax like:
This is called a "shebang" line.

For example:

Can have endless number of interpreters of source text. Can invent your own.

If no shebang line

It is usually possible to omit the shebang line, and your system will try (for example) to run the script in the simple Bourne shell sh.

But you cannot depend on this - exactly what happens depends on each system.
Your shell scripts may not be portable to different UNIX-family systems.

So it is good practice to define the shell in the first line.


Get the test programs above to work, and see your environment variables:

  1. shtest
    • on DCU Linux, runs in bash, because sh is bash (see above)
    • Some environment variables:
      no_proxy='localhost,, dcu.ie'
    • To change them:

  2. cshtest
    • on DCU Linux, runs in tcsh, because csh is tcsh (see above)
    • Some environment variables:
      cwd     /users/gdf1/mhtest09
      home    /users/gdf1/mhtest09
      path    (/users/gdf1/mhtest09/bin /usr/local/bin /usr/bin /bin ....)
      shell   /bin/tcsh
      user    mhtest09
    • To change them:
      set path = ( . $home/bin /bin /usr/local/bin .... )


In shtest, why not:
if test $1 = ""

Environment variables in parent and child processes

  1. Script cannot change parent environment:
    cd (change directory) in Shell script only has effect within that script.
    In general, environment variables changed within Shell script (such as "cwd" or "PWD" - current working directory) are only changed for the duration of that script.

  2. Use alias to change current process's environment variables:
    Instead of a Shell script, use an alias, which is a command-line text substitution.

  3. Script's variables are not usable by children (scripts called by that script) by default:
    You need to "export" them to make them available to children.
    Example: prog1:

    export x
    echo "prog1 [$x]"

    calls prog2:

    echo "prog2 [$x]"

    Try it with and without export.
    export will make variable x available to a script called by this script, and any scripts called by that script, etc., without any further exporting needed. Only a single export statement is needed (in the top parent script).

  4. Functions in the same script can access variables:
    Note that environment variables are by default available to functions within the same Shell script without exporting.

Caching and re-building the PATH

It would be quite an overhead to search all the directories in the PATH every time you type a command.
So some (all?) shells make a list of executable files in these directories (with the exception of ".") once, at login, or at the start of running a script, and then the shell caches that list in memory for future use.

This can cause some problems. e.g. You add a new program to your $HOME/bin directory, which is in the PATH. You then type the name of the program and it is not recognised. The solution is you need to re-build the cache.

How to re-build the cache of the PATH

  source .FILE
where .FILE is the config file where the path is defined.

If you do this a lot, you might like to put the following alias in .bashrc:

  alias redo="source $HOME/.bashrc"
or .cshrc:
  alias redo    source $home/.cshrc
And then, every time your PATH cache is out of date, you type:

CDPATH (jumping around disk)

CDPATH is very useful.
It is a quick way of jumping around the disk.

On bash, in .bashrc:
export CDPATH=$HOME:$HOME/public_html
On csh, in .cshrc:
set cdpath = ( $home $home/public_html )

Q. Do you need "." in the CDPATH as well?

If you have defined "cdpath" like above, then wherever you are on the disk, you can jump direct to a subdirectory off your home directory or off your web directory by just typing "cd (subdirectory)".

Other tools for jumping around disk:

Using tools like this, jumping around the disk and performing tasks on the command-line can actually be quicker than doing it through the File Manager.


echo '\0nn'       echo char by octal numeric code  
echo '\0nnn'       

echo "\0115"      echo "M"
echo "\0275"      echo "½"

echo -e (string)   



The above echo syntax works on csh on DCU Linux.

Get this script working on csh on DCU Linux: Script to build list of chars

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.