shell-command-to-string
可以执行shell命令返回字符串,比如(shell-command-to-string "ls")
可以正常执行返回。
但是对于bash builtin,例如alias, complete,(shell-command-to-string "alias")
返回为空字符串。
我试着用make-process来实现,水平太渣,半天没写出来。
请问各路高手,怎样用elisp执行builtin命令并返回字符串?
shell-command-to-string
可以执行shell命令返回字符串,比如(shell-command-to-string "ls")
可以正常执行返回。
但是对于bash builtin,例如alias, complete,(shell-command-to-string "alias")
返回为空字符串。
我试着用make-process来实现,水平太渣,半天没写出来。
请问各路高手,怎样用elisp执行builtin命令并返回字符串?
试试 bash -i -c alias
(shell-command-to-string "bash -i -c alias")
;; =>
"bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
alias l='ls -l'
alias la='ls -al'
"
(shell-command-to-string "bash -i -c alias 2>/dev/null")
;; =>
"alias l='ls -l'
alias la='ls -al'
"
alias 为空也可能是它真的没有内容:
(shell-command-to-string "alias")
;; => ""
(shell-command-to-string "source ~/.aliases; alias")
;; => "alias l1='ls -1'
;; alias la='ls -a'
;; alias ll='ls -lh'
;; ...
;; "
但即使 alias 有内容也不一定起作用:
(shell-command-to-string "source ~/.aliases; ll /")
;; => "/bin/bash: ll: command not found
;; "
(shell-command-to-string "bash --login -i -c 'source ~/.aliases; ll /'")
;; => "total 52
;; drwxrwxr-x+ 192 root admin 6.4K Oct 24 03:38 Applications/
;; drwxr-xr-x+ 67 root wheel 2.2K Jan 9 2017 Library/
;; drwxr-xr-x@ 2 root wheel 68B Apr 17 2016 Network/
;; drwxr-xr-x@ 4 root wheel 136B Jul 13 14:01 System/
;; drwxr-xr-x 8 root admin 272B Sep 18 19:30 Users/
;; ...
;; "
综合两位的答案,用了bash的-l, -i, -c选项,去除stderr输出(不造这是个啥?)
(shell-command-to-string "bash -lic alias 2>/dev/null")