emacs --eval '(ediff "$1" "$2")'如何将此行放入 shell 脚本中?
我想将以下行:
emacs --eval '(ediff "$1" "$2")'
放入shell脚本中,这样我就不需要每次都写这么长而复杂的行。但是,当我将其按原样放入 shell 脚本中时,出现错误:
ediff-find-file: File `$1' does not exist or is not readable
我猜这里存在某种 "
转义问题,我需要放入一些 \
在这一行中我到底需要修复什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是正常的,您的字符串用单引号括起来,并且变量不会在单引号内扩展。
您应该使用双引号,以便它们得到扩展,并转义字符串中现有的双引号:
值得注意的是,在扩展变量时,只有外部引号重要。即:
将打印
'foo'
。单引号包围$A
的事实并不重要:外引号是双引号。This is normal, your string is surrounded with single quotes, and variables are not expanded within single quotes.
You should use double quotes so that they get expanded, and escape existing double quotes in the string:
It is worth noting that when expanding variables, only the outer quotes matter. That is:
will print
'foo'
. The fact that single quotes surround$A
here does not matter: the outer quotes are double quotes.您面临的直接问题是单引号阻止了 shell 的所有扩展。您需要双引号才能发生任何类型的扩展。但是,您不能只编写
emacs --eval "(ediff \"$1\" \"$2\")"
因为 Emacs 无法正确引用文件名,因此您的脚本将如果文件名包含"
或\
,则中断。幸运的是,有一个非常简单的解决方案:
如果存在现有的 Emacs 进程,最好通过调用
emacsclient
不幸的是,对于 Emacsclient,--eval
将其所有参数视为 Lisp 表达式,因此您需要在 bash 或 ksh 中进行引用。使用 ${VARIABLE//PATTERN/REPLACMENT} 在双引号和反斜杠之前添加反斜杠;这有点痛苦,但它有效。Your immediate problem is that the single quotes prevent all expansion by the shell. You need double quotes for any kind of expansion to happen. However, you can't just write
emacs --eval "(ediff \"$1\" \"$2\")"
because the file names would not be properly quotes for Emacs, so your script would break if the file names contained"
or\
.Fortunately, there's a very simple solution:
It would be better to reuse an existing Emacs process if there is one, by calling
emacsclient
. Unfortunately, with Emacsclient,--eval
treats all its arguments as Lisp expressions. So you need to do the quoting inside the shell. In bash or ksh, you can use${VARIABLE//PATTERN/REPLACEMENT}
to add backslashes before the double quotes and backslashes; it's a little painful but it works.为了在我的
$HOME/.profile
中有一个名为“ediff”的简单函数,我将两个技巧结合在一起。来自 吉尔斯上面和来自权威博客。我为 emacsclient 和 emacs 添加了-c
参数作为后备编辑器:To have an easy function called "ediff" in my
$HOME/.profile
I combined two tips together. The one from Gilles above and from the defunitive blog. I added the-c
argument for emacsclient and emacs as the fallback editor: