C Shell Scripting/Modifiers

From Wikibooks, open books for an open world
< C Shell Scripting
Jump to: navigation, search

Variable Modifiers[edit]

In C shell, the path value obtained from a variable can be modified before it is used into a command or expression. Variable modifiers are given after a colon (:) at the end of a variable. The meaning of the modifiers are as follows:

:h returns the directory of a path (aka "head")
:t returns the filename of a path (aka "tail")
:r returns the directory and filename without the last extension (aka "root")
:e returns the extension of the path (aka "end")

For example, using this script:

#!/bin/csh -f
set file = /usr/joe/backup.tar.gz

echo $file:h
echo $file:t
echo $file:r
echo $file:e
echo $file:t:r:r
echo $file:h:h

will produce this:

/usr/joe
backup.tar.gz
/usr/joe/backup.tar
gz
backup
/usr

Lessons[edit]

  1. Variable modifiers are convenient to manipulate paths within the script.
  2. tcsh allows chaining modifiers together but some implementations only support one modifier at a time.
  3. Concatenating a variable with a colon character require code such as $var"": to avoid a "Bad : modifier" error.