0

I have

(shell-command-to-string "ls")

which works just fine but when I put it in a function it breaks as

(defun testing ()
  (shell-command-to-string "ls")
)

outputs nil. I am new to elisp so I am clearly missing something. Can someone point me in the right direction?

5
  • I cannot reproduce this. How are you calling the function testing? Commented Dec 15, 2021 at 19:10
  • Just testing() and running C-x C-e on the function itself then the calling of the function.
    – greenbug
    Commented Dec 15, 2021 at 19:23
  • To call the function, you have to write (testing) - if you do C-x C-e after that, does it work?
    – NickD
    Commented Dec 15, 2021 at 19:40
  • @NickD Thanks for that! Everything seems to work now. Want to make an answer so I can accept it?
    – greenbug
    Commented Dec 15, 2021 at 19:59
  • You should probably add the way that you tried to call the function to your question. Without that detail, the question is incomplete.
    – NickD
    Commented Dec 15, 2021 at 20:15

1 Answer 1

0

In Lisp, a function call is denoted like this: (func arg1 arg2 ...). IOW, the first element of the list is the function and subsequent elements are the (evaluated) arguments. Contrast this with most other languages where a function call is denoted like this: func(arg1, arg2, ...).

So you define the function like this:

(defun testing ()
  (shell-command-to-string "ls")
)

Doing C-x C-e after the closing paren evaluates the defun and adds the definition of the function to the system. Then to call the function you say:

(testing)

This calls the function testing with no arguments.

You should probably read the "Introduction to Programming in Elisp" which can be found here.

Your Answer

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

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