相当于bash中的python的textwrap deden [英] Equivalent of python's textwrap dedent in bash
本文介绍了相当于bash中的python的textwrap deden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在bash中有一个包含多行字符串的变量:
mystring="foo
bar
stack
overflow"
很明显,当Iecho "$mystring"
时,这会产生大量的缩进。在Python中,我只需导入textwrap并在字符串上使用deden,这就将我带到了这里。有没有为bash而存在的类似于python的dedent模块的东西?
推荐答案
您可以使用sed删除每行的前导空格:
$ sed 's/^[[:space:]]*//' <<< "$mystring"
foo
bar
stack
overflow
替代您可以(ab)use the fact that read will remove leading and trailing spaces:
$ while read -r line; do printf "%s
" "$line"; done <<< "$mystring"
foo
bar
stack
overflow
在您基本上想要删除所有空格的示例中:
$ echo "${mystring// }"
foo
bar
stack
overflow
这篇关于相当于bash中的python的textwrap deden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文