简体   繁体   English

org-mode babel sh脚本+转义(?)〜主目录的char?

[英]org-mode babel sh script + escaped(?) ~ char for home directory?

I have this small sh script under org-mode babel: 我在组织模式babel下有一个小的sh脚本:

#+NAME: testHomeDir 
#+BEGIN_SRC sh :var directory="./"
set -e
cd $directory
ls | head -5
#+END_SRC

Note: set -e is there to stop the script as soon as an error occurs. 注意: set -e在出现错误后立即停止脚本。

The script works well with full path: 该脚本在完整路径下效果很好:

#+CALL: testHomeDir("/home/picaud/Temp")
 #+RESULTS: :RESULTS: current issues.pdf AnnotatedPDF Bitbucket compareScript.wls Data :END: 

However when used with ~ (my home directory) it does not work anymore: 但是,当与~ (我的主目录)一起使用时,它不再起作用:

#+CALL: testHomeDir("~/Temp")

I get the 我得到了

sh: 4: cd: can't cd to ~/Temp sh:4:cd:无法cd到〜/ Temp

error message. 错误信息。

Please also note that 另请注意

#+CALL: testHomeDir("\~/Temp")
#+CALL: testHomeDir("\\~/Temp")

do not work too. 也不行。

I guess that the ~ character is escaped somewhere (in Emacs? in sh?)... 我猜~字符转义了某个地方(在Emacs中?在sh中?)...

My question is: how to fix that? 我的问题是:如何解决?

The ~ is not being expanded by the shell because it is being set within Emacs and is the literal string '~'. 〜不会被shell扩展,因为它是在Emacs中设置的,并且是文字字符串'〜'。 In order to get the expansion you need to run it through eval: 为了获得扩展,您需要通过eval运行它:

directory=$(eval echo $directory)

before you try to use it. 在尝试使用它之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM