0

I have the following Problem, I need an environment variable that contains globbing characters as clear text, and I can't seem to escape them. Escaping with backslash works, but for some reason the backslash is still part of the string."

Disabling GLOB_SUBST resolves the problem, but I´d like to avoid that.

I used the following to narrow down the problem:

touch foo; chmod +x ./foo

glob expansion i.E. $test contains all files in the directory.

echo 'export test="*"; echo $test' > foo; source ./foo

no glob expansion, but the escape character is added to the string.

echo 'export test="*"; echo $test' > foo; source ./foo

output: *

Any help would be appreciated.

Yankas
  • 3

1 Answers1

1

In the first case, you assign the string * to the variable, and since globsubst is enabled, the unquoted expansion $test is subject to globbing (as in a POSIX shell). In the second case, you assign the string \*, but it's not expanded as a glob since the backslash escapes the asterisk. (Otherwise zsh would complain that there were no matching files found, unless you've disabled that, too. You probably don't have filenames starting with a backslash anyway, but the glob to match them would have to be \\*.)

To stop the globbing, quote the variable expansion, i.e. echo "$test".

See: When is double-quoting necessary?

Also, you don't need export if you're just going to use the variables within the same shell (even between the main shell and the sourced script).

ilkkachu
  • 138,973