Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

Quoting

Oregon Coast

Quoting
Previous | Next

Topics

Double quotes
Single quotes
Shell escape character
Quoting just part of a word
Wildcarding with special characters
See also

The shell has several quoting mechanisms for marking a section of a command for special processing. One of them, command substitution, which uses the `...` or ``...`` syntax, is discussed in the chapter on i/o redirection.

The other quoting mechanisms focus more simply on the problem of overriding the special meanings that certain characters have.

Double quotes

Double quotes are of use when you want to pass a character string containing a space or other word separator to an application. Normally, if you called a C program, it would see these words as separate argv entries instead of a single character string; double quotes prevents this breakup into words. We can demonstrate this using the simple myecho program in the Samples directory which prints out the argv list it receives with single quotes around each entry:

257 D% cd ~\samples 258 D% myecho hello world 'myecho' 'hello' 'world' arg length = 19 characters 259 D% myecho "hello world" 'myecho' 'hello world' arg length = 19 characters

Double quotes also turn off the special meaning of the various wildcard characters and the single quote:

260 D% echo "* isn't a wildcard character inside quotes" * isn't a wildcard character inside quotes 261 D% echo "~" ~ 262 D% _

Command, history and variable substitutions inside double quotes are still done:

262 D% echo "*** The home directory is $home ***" *** The home directory is d:\Nicki *** 263 D% echo "`echo ~`" d:\Nicki 264 D% echo "myecho.c is `wc -l < myecho.c` lines long" myecho.c is 24 lines long 265 D% echo "!?samples" echo "cd ~\samples" cd ~\samples

Single quotes

Single quotes are a little more brute force way to turn off special meanings. Wildcards, variables and command substitutions are all treated as ordinary text. Only history references are recognized inside single quotes.

266 D% echo '*' * 267 D% echo '$cwd' $cwd 268 D% echo '`echo hello`' `echo hello` 269 D% echo '!?samples' echo 'echo "cd ~\samples"' echo "cd ~\samples" 270 D% _

It is not necessary to quote an entire word. It’s possible (and often useful) to selectively quote just as much as desired. The quoting characters are processed out just before invoking the command. Example:

270 D% echo 'no'w is "t"h'e' `echo time` now is the time

Shell escape character

The shell escape character is normally the circumflex, ^. It has two uses: preceding any of the special characters, it turns off that special meaning. When followed by an alphabetic character or hex or octal number, it can be used to enter binary data or characters that couldn’t easily be typed. To get a literal escape character, type two escapes in a row. These specific escape sequences have special meaning:

^a Audible Alert (Bell)
^r Carriage Return
^b BackSpace
^t Tab
^f Form Feed
^v Vertical Tab
^n NewLine
^^ Single escapesym

At the very end of a line, the escape has a special meaning: the next line is a continuation line. Inside a quoted string, the ^-newline combination will be replaced with a simple newline; anywhere else, the combination is just turned into a space. The other special case is when it immediately follows[. Since [^...] is a wildcard exclusion range, the ^ in this case is treated as a literal character so you won’t have to type two of them in a row.

Escape characters work even inside single or double quotes.

271 D% echo now ^ is the time now is the time 272 D% echo "now ^ is the time" now is the time 273 D%

Quoting just part of a Word

It’s possible to combine the quoting mechanisms or use them on just the part of a string you want quoted. For example,

273 D% echo '$cwd='$cwd $cwd=d:\Nicki

Here’s another example, searching through a series of .csh files, looking for those that are self-loading procedures. For example, we can spot that whereis.csh is self-loading because it contains a line like this:

whereis $argv

To look for occurrences of this sort, we might loop through a list of .csh files, grep’ing each for the filename (minus the directory and .csh extension) followed by white space followed by $argv:

274 D% foreach i (~\samples\*.csh) 275 D? grep $i:b'[ ^t]*$argv' $i 276 D? end bits $argv bumpdate $argv caldate $argv calendar $argv :

Notice how the $i:b part is outside the quotes so that the filename can be substituted in and edited to strip off the directory and extension. Conversely, the [ ^t]* and $argv portions are inside the quotes to avoid having them confused as a wildcard or variable substitution, respectively.

Wildcarding with special characters

If you’d like to wildcard filenames that have literal $, [, quote or other special characters, you’ll have to quote or escape the special characters to turn off their special meanings. For example,

277 D% ls g:\tmp $abc [hello this name has spaces 278 D% ls '$'* $abc 279 D% ls *^ * this name has spaces

See also

Quoting
History recall
Variable substitution
Order of evaluation
Tutorial: The history mechanism
Tutorial: Variable substitutions
Tutorial: Command substitution

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.