Reading: ...
In my Unix Overview document read the sections on "Shell and Environment Variables" and "Quoting" in the Bourne Shell chapter. Note that my document uses all upper case letters for shell variables and environment variables. The more modern convention is to use lower case (or mixed case) for shell variables, reserving all uppercase for environment variables.
In this lab you will experiment with some basic shell concepts.
As you work through the exercises below, record the commands you used and make any other notes that seem relevant in a document that you can submit later.
Use the touch command to create files with the following names by quoting them. Note that "touching" a file that does not exist causes it to be created as an empty file. If you touch a file that does exist, the date/time of last modification is updated to the current date/time.
Remove all the files you created above, but this time use the backslash to escape the special meaning of each meta-character individually (i.e., don't quote the entire name).
Enter the following program into a file named showargs.c:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char **argv )
{
int counter = 0;
while( *argv ) {
printf( "%2d: %s\n", counter++, *argv++ );
}
return EXIT_SUCCESS;
}
Compile this program using the following command:
$ gcc -o showargs showargs.c
You may have to first install the C compiler. Use the following command for that:
$ sudo apt install build-essential
Now run the following commands and explain what you are seeing:
Repeat the last command above after doing shopt -s failglob to see the effect of setting the "failglob" shell option.
Create a shell variable named working with a value of Homework-2025-09-12.txt. Use echo to display it.
Run a subshell by invoking bash again. Is the shell variable present in the subshell? How did you check?
Exit from the subshell and then create an environment variable named WORKING with the same value as before. Here we follow the convention of using upper case names for environment variables.
Run a second subshell and verify that the environment variable was inherited into that subshell.
Exit from the subshell and remove both the shell variable and environment variable using unset. Verify they are no longer present. How did you check that they were deleted?
The date command is a useful command for generating the date in various formats. Review the manual page with particular attention on the various format string specifiers. The specifier of interest to use right now is %F which outputs the date in ISO-8601 format (YYYY-MM-DD). This format has the advantage that a simple text sort will sort dates in chronological order.
Create a file with the name notes-2025-09-12.txt except generate the date using the `date` command and command substitution (i.e., either with $(...) or `...`, although the dollar-sign syntax is more modern and generally the best to use). Note that the command you use will work for any day and would thus be suitable for using in a script.
What command will create a file like peter's-notes-2025-09-12.txt? Here you can expand the value of $USER to get the current username. Be mindful of the single quote in the name (note that double-quoted strings still expand embedded $-macros.
Image that you would like to let users specify the format of the date in the filename. Specifically, let users set an environment variable DATE_FORMAT to be a format string. Then use that format string instead of %F in your command from the previous question. Try using the format string %FT%T (The 'T' delimiter between the date and time is required by ISO-8601).
Keep in mind that some of the features you experimented with in this part are primarily useful in shell scripts. They may seem like overkill for interactive use (and probably are), but they are essential when building general-purpose, reusable scripts.
For this lab submit a document that details the steps you took and the commands you used to answer each question. The lab is worth 20 points.
Last Revised: 2025-09-12
© Copyright 2025 by Peter Chapin <peter.chapin@vermontstate.edu>