-4

This works beautifully in BASH:

$ where=/sys/class/backlight/*
$ echo $where
/sys/class/backlight/intel_backlight

However when put in the POSIX script with /bin/sh as an interpreter, this doesn't work, where becomes /sys/class/backlight/*.

I've tried to Google but probably I'm not using the right terms.

1 Answers1

4

The value of the variable where is always the string /sys/class/backlight/*. Generally speaking, there is no wildcard expansion in contexts where the syntax expects a single word, such as the right-hand side of a scalar assignment. There is wildcard expansion (and also word splitting) in contexts where the syntax expects a list of words, such as a command and its arguments, or the right-hand side of an array assignment. See Expansion of a shell variable and effect of glob and split on it and When is double-quoting necessary? for more details.

$where expands the wildcard if it's used in a list context. Presumably, in your script, you put $where in double quotes (which makes the whole thing a single word) or in some other word context.

If your script is a bash (not plain sh) script, you can make where an array:

where=(/sys/class/backlight/*)

Then you can use "${where[@]}" for the list of directories for backlight devices. Strangely, ksh made $where be equivalent to ${where[0]}, i.e. taking the first element of the array and ignoring the rest, so if you're assuming that there's a single device, you can just keep using $where in a word context.