0

I have a shell script

$ cat ~/foo.sh

#!/bin/bash

echo $1 $2

I'm trying to call this from emacs. My elisp function is

(defun foo (word1 word2)
  "Print these two words."
  (interactive
   (list
    (read-string "Enter first word: ")
    (read-string "Enter second word: ")))
  (shell-command-to-string "bash ~/foo.sh %s %s" word1 word2))

This, however, yields an error. What am I doing wrong?

  • 2
    "Yields an error" isn't terribly helpful. A good question quotes the exact error message. – phils Dec 21 '16 at 10:44
1

You can use format :

(defun foo (word1 word2)
  "Print these two words."
  (interactive
   (list
    (read-string "Enter first word: ")
    (read-string "Enter second word: ")))
  (shell-command-to-string (format "~/foo.sh %s %s" word1 word2)))
2

C-hf will tell you that (shell-command-to-string COMMAND) is the general form for that function, so it is clear that (shell-command-to-string "bash ~/foo.sh %s %s" word1 word2) is not valid.

Use format as Håkon Hægland has shown, but you should always process your arguments with shell-quote-argument to ensure the formatted shell command is correct.

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.