C Shell Scripting/Modifiers

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Variable Modifiers[edit | edit source]

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 | edit source]

  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 requires code such as $var"": or ${var}: to avoid a "Bad : modifier" error.