bash 在bash中将一个字符串切成几行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:50:19  来源:igfitidea点击:

cutting a string into several lines in bash

bashcut

提问by SIMEL

I want to take the path of the local directory and put each directory on the path in a different line. I've tried to do it using cut:

我想取本地目录的路径并将每个目录放在不同行的路径上。我尝试使用 cut 来做到这一点:

pwd | cut -f 1- -d\/ --output-delimiter=\n

pwd | cut -f 1- -d\/ --output-delimiter=\n

but it doesn't change the '/'s into EOL, but puts n's instead. What am I doing wrong?

但它不会将 '/' 更改为 EOL,而是将 n 改为。我究竟做错了什么?

回答by aioobe

This should do the trick

这应该可以解决问题

pwd | tr '/' '\n'

If you don't want an empty line in the beginning (due to the initial /) you could do

如果你不想在开头有一个空行(由于初始/),你可以这样做

pwd | cut -b2- | tr '/' '\n'

Example:

例子:

#aioobe@r60:~/tmp/files$ pwd
/home/aioobe/tmp/files
#aioobe@r60:~/tmp/files$ pwd | cut -b2- | tr '/' '\n'
home
aioobe
tmp
files

回答by codaddict

You can try:

你可以试试:

pwd | tr '/' '\n'

回答by Paused until further notice.

This is how you would accomplish what you set out to do (using ANSI-C quoting):

这就是您将如何完成您打算做的事情(使用 ANSI-C 引用):

pwd | cut -f 1- -d\/ --output-delimiter=$'\n'