Directory for This Exercise

Continuing a Command on the Next Line

The greater than symbol, > , that you see after all but
the last time you hit Enter is a secondary prompt.

It tells you that the shell is awaiting more input.

This will not work unless you hit the Enter key immediately
after typing the backslash.

More practice with backslash

echo The University \[Enter]
of Massachusetts \[Enter]
at Boston [Enter]

Try typing a space before hitting Enter after backslash

echo Hello \[Space][Enter]

The echo command does not wait for further input, it prints
its argument and you don't get a secondary prompt.

The backslash is a quoting character that turns off the special
meaning of whatever character comes next.

When you hit Enter
immediately after the backslash, you are turning off the
special meaning of the new line character, which tells the shell
to execute a command.

If you put a space, or any other character
immediately after the backslash, that is the character that the
backslash applies to, not the new line character which the shell
receives after you hit Enter.

Changing Prompts

Change your primary prompt

PS1='===> '

Note the single quotes.

You must use them

Since you have not added this to your .bash_profile, this will only be
your prompt for this terminal session.

Change your secondary prompt

PS2='>> '

Show your new secondary prompt

echo Go \[Enter]
Red \[Enter]
Sox [Enter]

You should see your new secondary prompt with each new line.

Semicolon, ;, to Separate Commands

Use three echo commands on the same command line

echo foo; echo bar; echo bletch

Each echo command generates its own line of output.

Use three more echo commands on the same line

echo Unix; echo is not; echo for the faint of heart

Use different commands on the same line

echo; echo This is what is in my home directory:; ls -al ~

& to Separate Commands

Copy hi_bye.sh from ~ghoffman/course_files/it244_files

cp ~ghoffman/course_files/it244_files/hi_bye.sh .

Display the contents of hi_bye.sh

cat hi_bye.sh

Run multiple instances of hi_bye.sh with & as a separator, then run the jobs command

./hi_bye.sh & ./hi_bye.sh & jobs

The ampersand causes each of the two invocations of hi_bye.sh to run in the background.

This means the ampersand has two functions, to cause a command to run in the
background and to separate one command from the next.

Notice that jobs is able to report on the two invocations of hi_bye.sh running
in the background, because it is running in the foreground.

Wait for completion of jobs

After the second Goodbye appears, hit Enter.

You should see a report that the two jobs have terminated.

Important Keyword Shell Variables

Display PATH

echo $PATH

Display HOME

echo $HOME

Display SHELL

echo $SHELL

Display PS2

echo $PS2

Display some of your global variables

env

Local Variables

Create a local variable

greeting=Hello

Display the local variable you just created

echo $greeting

Assign a string with a space in it to this local variable

greeting='Hello there'

Display the new value of this variable

echo $greeting

Copy print_foo.sh from
~ghoffman/course_files/it244_files

cp ~ghoffman/course_files/it244_files/print_foo.sh .

Print this script to the screen

cat print_foo.sh

This script simply prints the value of the variable foo to Standard Output.

Create the local variable foo

foo=FOO

Print the value of foo

echo $foo

Run print_foo.sh

./print_foo.sh

The script prints no value for foo,
since foo was a local variable defined
in your original shell.

The script runs in a subshell and cannot see
the local variables of its parent shell.

Global Variables

Set foo to a new value and export it

export foo=BAR

Print foo in your current shell

echo $foo

Run print_foo.sh

./print_foo.sh

Since the export command makes foo a global variable,
print_foo.sh can now see this variable in the subshell in which
it is run.

The Directory Stack

Go to ~ghoffman/course_files/it244_files/dir_tree

cd ~ghoffman/course_files/it244_files/dir_tree

Look at your current location

pwd

Look at the contents of the directory

ls

Use dirs to look at the directory stack

dirs

It only lists the current directory.

Go down one level using pushd

pushd proj

Two directories are now listed.

Display your location

pwd

Look at the contents of the directory

ls

Go down another level

pushd proj1

Three directories are now listed.

Display your location

pwd

Go up one level using popd

popd

Now, only two directories are listed, because you poped the previous directory from the stack.

Display your location

pwd

Go up one level, suppress output and get location, all on one line

popd > /dev/null; pwd

By redirecting output to /dev/null, you have stopped popd from printing out the directory stack.

By using a semi-colon followed by pwd, you have displayed your current location
without having to wait for a prompt.

Bash Script for This Exercise

Go back to your ex18 directory

cd ~/it244/ex/ex18

Create the script ex18.sh

nano ex18.sh

Write into this file commands from certain of the sections above

Include Unix commands from the section Important Keyword Shell variables,
up to and including the section The Directory Stack.