String Editing (Colon) Operators
When the C shell and bash do history substitutions () they can also edit the substitution. The C shell - but not bash - can also edit variable substitutions (). For instance, in the first example below, when !$
contains /a/b/c
, adding the "head" operator :h
will give just the head of the pathname, /a/b
.
For a complete but very terse list of these operators, see the csh manual page. We hope the examples below will help you understand these useful operators.
:h
gives the head of a pathname (), as follows:
%
echo /a/b/c
/a/b/c %echo !$:h
echo /a/b /a/bThat took off the filename and left the header. This also could be used with C shell variables () as:
%
set x = /a/b/c
%echo $x
/a/b/c %echo $x:h
/a/b
:r
returns the root of a filename:
%
echo xyz.c abc.c
xyz.c abc.c %echo !$:r
echo abc abcThe
:r
removed thec
from the last argument, leaving the root name. This could also be used in C shell variable names:%
set x = abc.c
%echo $x:r
abc
:g
For more than one name, you can add the g operator to make the operation global. For example:(...)
%
set x = (a.a b.b c.c)
%echo $x:gr
a b cThe
:gr
operator stripped off all dot () suffixes. By the way, this use of g does not work with the history commands.:e
returns the extension (the part of the name after a dot). Using csh variables:
%
set x=(abc.c)
%echo $x:e
cNo luck using that within history, either.
:t
gives the tail of a pathname - the actual filename without the path:
%
echo /a/b/c
/a/b/c %echo !$:t
cWith csh variables:
%
set x=(/a/b/c)
%echo $x:t
cAnd with multiple pathnames, you can do it globally with:
%
set x=(/a/b/c /d/e/f /g/h/i)
%echo $x:gt
c f iWhile the corresponding heads would be:
%
set x=(/a/b/c /d/e/f /g/h/i)
%echo $x:gh
/a/b /d/e /g/h
:p
prints the command, but does not execute it ():
%
echo *
fn1 fn2 fn3 %!:p
echo fn1 fn2 fn3
:q
prevents further filename expansion, or prints the command as is:
%
echo *
fn1 fn2 fn3 %!:q
echo * *The first command echoed the files in the directory, and when the
:q
was applied, it echoed only the special character.:x
is like:q
, but it breaks the line into words. That is, when using:q
, it is all one word, while:x
will break it up into multiple words. [:q
and:x
are more often used with C shell arrays (). -JP ]
- DR