Consider a script file: ~/project/script.sh

echo "arg 1 $1"  
echo "arg 2 $2"
echo "arg 3 $3"

And elisp file that invokes it using make-process: ~/project/special-mode/special-mode.el

(let* ((script-file  (shell-quote-argument (expand-file-name "~/project/script.sh")))
       (argA          (shell-quote-argument "A"))
       (argB          (shell-quote-argument "B")))
             :name            "special-mode"
             :buffer          "*SPECIAL-MODE*"
             :command         `( "sh" "-c" ,script-file ,argA  ,argB )
             :connection-type 'pipe
             :sentinel   nil)))


The function evaluates but the arguments to the shell script (argA and argB) are never passed. Any idea how to fix this?

Emacs Version

GNU Emacs 28.0.50



As pointed out by xuchunyang, adding a shebang line to the script.sh and calling it like an ordinary executable is a much cleaner solution. That is:

echo "arg 1 $1"  
echo "arg 2 $2"
echo "arg 3 $3"

Old Solution

Removing the -c flag fixed the issue. The -c flag is used to call shell commands in-line(and probably more).

sh -c[-abCefhimnuvx][-o option][+abCefhimnuvx][+o option]command_string
               [command_name [argument...]]

From The Single UNIX ® Specification, Version 2

-c        Read  commands from the command_string operand. Set the value of
          special parameter 0 (see Special Parameters ) from the value  of
          the  command_name operand and the positional parameters ($1, $2,
          and so on) in sequence from the remaining argument operands.  No
          commands shall be read from the standard input.
  • You can just execute the program directly without sh by adding a shebang and making it executable. sh -c 'echo $0 $1' app_name arg1 works while sh -c 'echoargs' app_name arg1 doesn't because as you mentioned the former is a inline script, but the later the program echoargs will run in a new process with its own arguments. – xuchunyang May 28 '20 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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