How can I get the shell's read command working (prompting for the input) in an elisp script? I have

(shell-command "read -s-p \"Enter Password: \" PASSWORD")

but it doesn't prompt anything.

  • 1
    Do you want to get an password in elisp? Then you may want to use read-password, see Reading a Password for documentation. – theldoria Jun 27 '17 at 7:57
  • @theldoria Thank you. I'd like also to learn how to call little pieces of interactive shell script in elisp scripts. – Gabriele Nicolardi Jun 27 '17 at 8:45

It is not that easy, because shell-command and start-process do not define an INPUT stream. call-process just operates on file input.

Emacs documentation states how to feed input to processes, see here.

Eventually you have to do something like this:

(defun endless/send-input (input &optional nl)
    "Send INPUT to the current process.
Interactively also sends a terminating newline."
    (interactive "MInput: \nd")
    (let ((string (concat input (if nl "\n"))))
        ;; This is just for visual feedback.
        (let ((inhibit-read-only t))
            (insert-before-markers string))
        ;; This is the important part.
         (start-process "rete" "*rete*" "bash" "-c" "read foo; echo $foo")

Evaluate and call this function. After inputting some keys and <enter>, have a look at buffer *rete*.

Note: read is a so called shell internal command. This means there exists no binary (i.e. /bin/read or /usr/bin/read). You need to call a shell (in this example bash) to use read.

Note: This code does not hide user input like your password example. You should not copy passwords with methods like this, imho this is highly insecure.

Note: big part of this code is shamelessly stolen from Endless Parentheses.

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.