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?

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

2 Answers 2

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)))
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.