如何将$ PATH的内容分成不同的行? [英] How to split the contents of `$PATH` into distinct lines?
问题描述
假设 echo $ PATH
产生/first/dir:/second/dir:/third/dir
.
问题:一个如何一次回显一个目录中 $ PATH
的内容,如:
Question: How does one echo the contents of $PATH
one directory at a time as in:
$ newcommand $PATH
/first/dir
/second/dir
/third/dir
最好,我试图找出如何使用 for
循环来执行此操作,该循环为 $中的目录的每个实例发出一个
. echo
实例PATH
Preferably, I'm trying to figure out how to do this with a for
loop that issues one instance of echo
per instance of a directory in $PATH
.
推荐答案
echo "$PATH" | tr ':' '\n'
应该做到这一点.这将简单地使用 echo"$ PATH"
的输出,并用换行符分隔符替换任何冒号.
Should do the trick. This will simply take the output of echo "$PATH"
and replaces any colon with a newline delimiter.
请注意, $ PATH
周围的引号可防止在 $ PATH
的输出中多个连续空格的折叠,同时仍输出变量的内容.
Note that the quotation marks around $PATH
prevents the collapsing of multiple successive spaces in the output of $PATH
while still outputting the content of the variable.
这篇关于如何将$ PATH的内容分成不同的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!