I've got this code:

(defun insert-random-uuid ()
  (shell-command "uuidgen" t))

which produces a uuid that looks like this ffc715d2-1e10-4faf-b1f6-f85cae88a7d7. However, I believe it's throwing in a newline and that it isn't returning a string:

(stringp (insert-random-uuid))

which returns nil

So I find this at the EmacsWiki:

(defun chomp (str)
      "Chomp leading and tailing whitespace from STR."
      (while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
        (setq str (replace-match "" t t str)))

which seems to handle both whitespace and \n. But of course (chomp (insert-random-uuid) gives an error, no doubt because insert-random-uuid is not returning a true string. So what is it returning, and how can I remove the newline? I tried various things to "cast" the output to string, but no luck. How can I get rid of this newline?

  • 4
    you wanted shell-command-to-string.
    – npostavs
    Commented Nov 9, 2015 at 5:17

1 Answer 1


Use (type-of EXPRESSION) to determine the type of an expression.

In your case you are confusing the value being inserted into a buffer by a function with its return value.

shell-command inserts the textual output of the command, but it returns the exit status of the command (which, of course, is an integer).

The simplest solution to your problem is probably to prevent the shell from generating a newline in the output in the first place. e.g.:

(shell-command "printf %s \"$(uuidgen)\"" t)
  • shell-command-to-string might be part of a solution too
    – YoungFrog
    Commented Nov 10, 2015 at 6:02
  • Perhaps, but that's not obvious to me. As far as I could tell from the question, the problem the user had with their original code was that it inserted an extraneous newline. The only reason they were ever attempting to manipulate the return value as a string was because they thought it would help deal with that problem. With the problem taken care of at the shell command level, I presume there's no requirement to return a string.
    – phils
    Commented Nov 10, 2015 at 6:34

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.