2

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?

Drew
  • 77,472
  • 10
  • 114
  • 243
Brian Fitzpatrick
  • 2,325
  • 1
  • 20
  • 42
  • 2
    "Yields an error" isn't terribly helpful. A good question quotes the exact error message. – phils Dec 21 '16 at 10:44

2 Answers2

3

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.

phils
  • 50,977
  • 3
  • 79
  • 122
2

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)))
Håkon Hægland
  • 3,648
  • 1
  • 22
  • 51