Explain the following unexpected result:

$ whereis date
date: /bin/date ...
$ echo $PATH
$ cat > date
echo "This is my own version of date."
$ date
Tue May 24 11:45:49 PDT 2005


What are two ways you can execute a shell script when you do not have execute access permission for the file containing the script? Can you execute a shell script if you do not have read access permission for the file containing the script?


What is the purpose of the PATH variable?

  1. Set the PATH variable so that it causes the shell to search the following directories in order:

    • /usr/local/bin

    • /usr/bin

    • /bin

    • /usr/kerberos/bin

    • The bin directory in your home directory

    • The working directory

  2. If there is a file named doit in /usr/bin and another file with the same name in your ~/bin, which one will be executed? (Assume that you have execute permission for both files.)

  3. If your PATH variable is not set to search the working directory, how can you execute a program located there?

  4. Which command can you use to add the directory /usr/games to the end of the list of directories in PATH?


Assume that you have made the following assignment:

$ person=jenny

Give the output of each of the following commands:

  1. echo $person

  2. echo '$person'

  3. echo "$person"


The following shell script adds entries to a file named journal-file in your home directory. This script helps you keep track of phone conversations and meetings.

$ cat journal
# journal: add journal entries to the file
# $HOME/journal-file

date >> $file
echo -n "Enter name of person or group: "
read name
echo "$name" >> $file
echo >> $file
cat >> $file
echo "----------------------------------------------------" >> $file
echo >> $file

  1. What do you have to do to the script to be able to execute it?

  2. Why does the script use the read builtin (page 927) the first time it accepts input from the terminal and the cat utility the second time?


Assume that the /home/jenny/grants/biblios and /home/jenny/biblios directories exist. Give Jenny's working directory after she executes each sequence of commands given. Explain what happens in each case.

  1. $ pwd
     $ CDPATH=$(pwd)
    $ cd
    $ cd biblios

  2. $ pwd
    $ CDPATH=$(pwd)
    $ cd $HOME/biblios


Name two ways you can identify the PID number of your login shell.


Give the following command:

$ sleep 30 | cat /etc/inittab

Is there any output from sleep? Where does cat get its input from? What has to happen before the shell displays another prompt?

Advanced Exercises


Write a sequence of commands or a script that demonstrates that variable expansion occurs before pathname expansion.


Write a shell script that outputs the name of the shell that is executing it.


Explain the behavior of the following shell script:

$ cat quote_demo
twoliner="This is line 1.
This is line 2."
echo "$twoliner"
echo $twoliner

  1. How many arguments does each echo command see in this script? Explain.

  2. Redefine the IFS shell variable so that the output of the second echo is the same as the first.


Add the exit status of the previous command to your prompt so that it behaves similarly to the following:

$ [0] ls xxx
ls: xxx: No such file or directory
$ [1]


The dirname utility treats its argument as a pathname and writes to standard output the path prefixthat is, everything up to but not including the last component:

$ dirname a/b/c/d

If you give dirname a simple filename (no / characters) as an argument, dirname writes a . to standard output:

$ dirname simple

Implement dirname as a bash function. Make sure that it behaves sensibly when given such arguments as /.


Implement the basename utility, which writes the last component of its pathname argument to standard output, as a bash function. For example, given the pathname a/b/c/d, basename writes d to standard output:

$ basename a/b/c/d


The Linux basename utility has an optional second argument. If you give the command basename path suffix, basename removes the suffix and the prefix from path:

$ basename src/shellfiles/prog.bash .bash
$ basename src/shellfiles/prog.bash .c

Add this feature to the function you wrote for exercise 14.