1

Some Emacs packages (for example AUCTeX) pass a user specified shell command to the shell by:

(call-process shell-file-name nil 0 nil shell-command-switch the-user-specified-command)

which in case of MS Windows is usually equivalent to:

(call-process "cmdproxy" nil 0 nil "-c" the-user-specified-command)

How should I escape characters if I want to pass this shell command:

"print arg.py" "11 11" "22 22"

which runs a script (with a space in its basename print arg.py) with two arguments 11 11 and 22 22?

In the following call, cmdproxy just says Unable to initialize device PRN.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\"print arg.py\" \"11 11\" \"22 22\"")

For the following call, it says error: no program name specified.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\"\"print arg.py\"\" \"\"11 11\"\" \"\"22 22\"\"")

For the following, it says '\"print arg.py\"' is not recognized as an internal or external command, operable program or batch file.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\\\"print arg.py\\\" \\\"11 11\\\" \\\"22 22\\\"")

On the other hand, the following call succeeds in running the script with arguments 11 11 and 22 22.

(setenv "ABC" "\"print arg.py\" \"11 11\" \"22 22\"")
(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "%ABC%")

That can be a good workaround but it doesn't tell how to escape characters.

Contents of the script file print arg.py, for those who want to test:

import sys

if __name__ == "__main__":
    print "start"
    for arg in sys.argv[1:]:
        print "(" + arg + ")"
    print "end"
0

2 Answers 2

0

It seems you don't need a shell for that: just do (call-process "print arg.py" nil "foo" nil "11 11" "22 22").

1
  • That requires modifying the code of the package. Users of the package can only change the user option the-user-specified-command or make a feature request for a way around. AUCTeX does provide a way around, but there may be other packages that don't.
    – Jisang Yoo
    Jun 28, 2013 at 8:03
0

Escape the space in the command name with backslash and put the arguments into single quotes:

(call-process "cmdproxy"
          nil "foo" nil
          "-c"
          "print\ arg.py '11 11' '22 22'")
3
  • With "print\ arg.py '11 11' '22 22'", it says 'printarg.py' is not recognized .... With "print\\ arg.py '11 11' '22 22'", it says 'print\' is not recognized ....
    – Jisang Yoo
    Jun 29, 2013 at 10:32
  • I tested this with regular executable which has a space in its name. Since you have a python script, there is a python interpreter that gets called by cmdproxy with your script as an argument. Try putting the 'print arg.py' in single quotes as well. Which python do you have? Jun 29, 2013 at 21:25
  • My python is installed to C:\Python27 but I am not sure why the path to the Python executable would matter as the error message seems to be printed by cmd or cmdproxy. The full error message was 'printarg.py' is not recognized as an internal or external command, operable program or batch file. With "'print arg.py' '11 11' '22 22'", the error message is ''print' is not recognized as an internal or external command, operable program or batch file.
    – Jisang Yoo
    Jun 30, 2013 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.