-1

When, in zsh, I execute python -c 'print('howdy')' from the command line, it produces the following error.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'howdy' is not defined

However, this python -c 'print("howdy")' does not, and instead produces the output expected by me, namely, howdy (as a string) is sent to stdout?

I thought python was agnostic on single or double quotes

oguz ismail
  • 1
  • 16
  • 47
  • 69
TransferOrbit
  • 201
  • 2
  • 7

1 Answers1

5

Zsh is first parsing the command, following its own rules on quotes. To zsh, the command python -c 'print('howdy')' looks something like the following:

  • We're calling the program python
  • The first argument is -c
  • The second argument is 'print(' enclosed in quotes, followed by howdy, followed by ')' enclosed in quotes.

Zsh is "expanding" this second argument to 'print(howdy)'. If you run this command in python you get the error you describe.

Simon Brahan
  • 2,016
  • 1
  • 14
  • 22
  • 1
    The correct term is quote removal. `'print('howdy')'` is a single word, two parts of which are contained in single quotes. Once the shell is done processing the command line, the command name is identified as `python`, and two words `-c` and `print(howdy)` are identified as the arguments to the command. – chepner Aug 31 '21 at 13:43