How do I delete the newline from a process output?

浪尽此生 提交于 2019-12-01 03:08:47

I think you can do

(replace-regexp-in-string "\n$" "" 
              (shell-command-to-string "git rev-parse --show-toplevel"))

If you only want to remove a newline at the very end of the output, use

(replace-regexp-in-string "\n\\'" "" 
  (shell-command-to-string "git rev-parse --show-toplevel"))

The accepted answer also replaces pairs of newlines ("\n\n") in the output by single newlines ("\n"), because $ matches at the end of the string or after a newline, while \\' only matches at the end of the string.

Assuming it is stored in a variable output-string, the final newline at the end can be removed this way:

(substring output-string 0 -1)

With the shell-command way, it would look like this:

(substring
  (shell-command-to-string "git rev-parse --show-toplevel")
  0 -1)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!