On Sat, Apr 16, 2022 at 08:06:23AM +0800, wilson wrote:
> 
> 
> Greg Wooledge wrote:
> > if [ "$1" == on ]
> 
> this sounds strange. why a string doesn't need "" around in shell script?

Shell works by text substitution. If you have

  foo $bar $baz

the shell first replaces $bar and $baz by their values. Suppose bar is `123',
baz is `a multi-word string', then the next round is

  foo 123 a multi-word string

Then the shell splits the result in words, goes to look up `foo' in all the
places $PATH says it should look (yes, I've taken a HUGE shortcut here, chime
in to fill the voids ;-), and the command foo (say /usr/local/bin/foo) gets
to see four parameters: `123', `a', `multi-word' and `string'.

The quotes hold together things; different rules hold for single quotes (do
not substitute $vars) and double quotes (do substitute). So if you want to
invoke foo with two params, it is

  foo $bar "$baz"

If you don't know whether bar could contain a space and you also want to
have bar in one piece, better safe than sorry:

  foo "$bar" "$baz"

Text substitution systems were a thing in the 1970ies..1990ies. They do
have interesting properties, and survive in some odd places, the shells
being one (TeX, METAFONT, Tcl come to mind, too). And oh, the C preprocessor.

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature

Reply via email to