Tuesday Tiny Techie Tip
csh(1) variable modifiers
Keeping in mind that if you ever want to write a script
longer than a few lines, you shouldn't use csh(1),
there are some csh features that are useful when
used on the command line. This week we'll take a look at
the available variable interpolation modifiers.
If you've got a variable whose value is a pathname,
csh lets you easily manipulate the different parts
of the path.
% set thefile=/home/jeffy/bin/foo.bar
% echo $thefile
/home/jeffy/bin/foo.bar
The modifiers are appended to the variable interpolation
with a ":"
- To show just the path part of the variable contents,
append a "h" (for head)
% echo $thefile:h
/home/jeffy/bin
- To show just the filename part of the contents append a
"t" (for tail)
% echo $thefile:t
foo.bar
- To show all but the file suffix, append an "r"
(for root?)
% echo $thefile:r
/home/jeffy/bin/foo
- To show only the file suffix, append an "e"
(for end, I guess)
% echo $thefile:e
bar
You can also combine the operators, so if you wanted just
the filename without its extension:
% echo $thefile:r:t
foo
All of these are especially useful in combination with
command history substitution:
% cd /vobs/Rm/Config/configErrno.h
/vobs/Rm/Config/configErrno.h: Not a directory.
% cd !$:h
cd /vobs/Rm/Config
% vi !-2:$:t
vi configErrno.h
So in that string of commands, I screwed up and typed the
full path to the filename I wanted to look at instead of
just the directory. So I used ":h" to chop off
the file name so my cd would work.
Then I used "!-2" to reference the command before
the last command, the ":$" history modifier to
grab the last argument to that command, and finally, the
":t" modifier to grab just the filename portion of
that argument.
The best part about all these modifiers is it lets you type
stuff that looks like linenoise yet does what you want
;-)
Tuesday Tiny Techie Tip -- 17 December 1996
Forward to (01/07/97)
Back to (12/10/96)
Written by Jeff Youngstrom
Up to the TTTT index
Tuesday Tiny Techie Tips are all © Copyright
1996-1997 by Jeff Youngstrom. Please ask permission before
reproducing any of this material.