我正在尝试使用简单的 bash 和 UNIX 命令生成一些类似 PATH 的目录列表。我认为这个命令给出了我喜欢使用的输出:
$ find /usr/local -type d -exec dirname \{\} \; | sort -u | tr '\n' ':'
/usr:/usr/local:/usr/local/lib:/usr/local/lib/python2.6:/usr/local/lib/site_ruby:/usr/local/lib/site_ruby/1.8:/usr/local/lib/xemacs:/usr/local/share:/usr/local/share/emacs:/usr/local/share/emacs/23.2:/usr/local/share/sgml:/usr/local/share/xml:/usr/local/share/zsh:
但是,当我尝试使用命令替换来使用该表达式(例如,分配给变量)时,它会中断:
$ echo $(find /usr/local -type d -exec dirname \{\} \; | sort -u | tr '\n' ':' )
/usr /usr/local /usr/local/lib /usr/local/lib/python2.6 /usr/local/lib/site_ruby /usr/local/lib/site_ruby/1.8 /usr/local/lib/xemacs /usr/local/share /usr/local/share/emacs /usr/local/share/emacs/23.2 /usr/local/share/sgml /usr/local/share/xml /usr/local/share/zsh
有什么想法我做错了什么,或者我该如何以不同的方式实现我的目标?
答案1
检查环境变量的内容IFS
,即字段分隔符。它不是在表单中使用find
,而是在echo $(find)
表单中使用。
export IFS="-"
echo $(...)
比较:
export IFS=:
echo $(echo asdf:asdf:asdf)
> asdf asdf asdf
export IFS=;
echo $(echo asdf:asdf:asdf)
> asdf:asdf:asdf
export IFS=:
unset IFS
echo $(echo asdf:asdf:asdf)
> asdf:asdf:asdf