Main index

Introducing UNIX and Linux


Advanced shell programming

Overview
Sending and trapping signals
      Signal names
Functions
Aliases
The 'exec' mechanism
The 'eval' mechanism
Sending data across networks
      Sending printable characters
      Splitting files
Makefiles
Safe programming
Setting up a terminal
More on files
Miscellaneous utilities
Summary
Exercises

Answer to chapter 9 question 1

This is a straightforward function, just requiring two commands between the braces.

thisyear() {
   printf "This year is "
   date "+%Y"
   }

Alternatively, this could be done using echo:

thisyear() {
   echo "This year is $( date +%Y )"
   }

Note that the argument to date does not need to be enclosed in quotes, as in this case it contains no characters with special meaning to the shell. In the first solution they are included for clarity, in the second one they were omitted to avoid clashing with the quotes enclosing the argument to echo.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck