-1

I've got a third party script that receives command line arguments in this form:

$ ./script.sh --params a=123 b=456

There is a file xyz.txt with this contents:

c=3
d=4

I'm trying to use its contents as paremeters for the script, but the script complains about not receiving proper arguments. When I do set -x, I can see that each of my arguments is enclosed in single quotes (so the third party script cannot handle them)

$ set -x
$ ./script.sh --params $(cat xyz.txt)

.... +-zsh:20> cat xyz.txt +-zsh:20> ./script.sh --params 'c=3' 'd=4'

Tried in bash (also with set -x) and the result is as expected (no single quotes):

$ set -x
$ ./script.sh --params $(cat xyz.txt)

++ cat xyz.txt

  • ./script.sh --params c=3 d=4

This behaviour is not present if the values in the file do not contain an equals sign. E.g.

e-5
f-6

... +-zsh:20> ./script.sh --params e-5 f-6

I tried disabling as many setopt as I could without success.

How can I prevent Zsh from enclosing my arguments in single quotes when they contain equal sign (mimic Bash behaviour)?

txauma
  • 1

1 Answers1

3

There are no single quotes inserted into the arguments by the shell.

Proof:

$ cat file
c=3
d=4
$ printf 'Argument: >>%s<<\n' $(cat file)
Argument: >>c=3<<
Argument: >>d=4<<

This shows that the the two lines read from file are properly split into two separate arguments and handed to the printf utility without the addition of single quotes.

Notice how the output does not change when running under set -x:

$ set -x
$ printf 'Argument: >>%s<<\n' $(cat file)
+/usr/local/bin/zsh:11> cat file
+/usr/local/bin/zsh:11> printf 'Argument: >>%s<<\n' 'c=3' 'd=4'
Argument: >>c=3<<
Argument: >>d=4<<

The single quotes are not part of the arguments passed to your script. They are just inserted as a visual aid by the shell for the sake of the trace output (which is debugging output and which can not be used or interpreted as input to the shell). The zsh and bash shells formats their tracing output slightly differently, that's all.

If you are experiencing issues with running your script from the zsh shell, then these are not due to the shell modifying the data in the file.

Kusalananda
  • 333,661
  • = is also a special operator in the zsh syntax so needs quoted at least in some contexts (compare echo =ls vs echo '=ls'). In bash it's special as well in that it causes what follows to be subject to tilde expansion (at least when what's before looks like a valid variable name). You'll notice bash -xc 'echo +=~' or bash -xc 'echo a=\~' outputs echo 'a=~' and echo '+=~' (even if those quotes are not strictly necessary in that case). bash -xc 'a\=x' outputs a=x which is a bug though (as a=x would assign x to a instead of running the a=x command). zsh's OK. – Stéphane Chazelas Oct 23 '20 at 17:24