Expansion of variables in sh scripts

Chris Torek chris at trantor.umd.edu
Sun Feb 14 19:51:33 AEST 1988


In article <1159 at valhalla.ee.rochester.edu> badri at valhalla.ee.rochester.edu
(Badri Lokanathan) writes:
>#!/bin/sh
>word=$1
>result=`awk "/^${word}/{print \$2}" datafile`
>echo $result
># This outputs the entire line, rather than the entry in the II column.

Yes.  Shell (sh, not csh) quoting is very easy to explain.  ""
quotes against file expansion; '' quotes against all expansion.
Any time a line is evaluated, one level of quoting is removed.
Backquotes evaluate the text inside the backquotes once.  Hence
"\$2" becomes $2 becomes nothing, and awk prints the whole line.

Note that this means you can use backquotes inside backquotes:

	foo=`eval echo \`basename ...\``

This is not directly possible in csh.
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris at mimsy.umd.edu		Path: not easily reachable



More information about the Comp.unix.questions mailing list