作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个非常简单的 bash 脚本,它提出的问题包括以前答案中的文本。例如:
questionText="Hi $userName, I'm going to ask you some questions. At the end of the process, I'll output a file that might be helpful to you."
echo "$questionText"
其中一些问题很长。 echo
的输出在我的 macOS 终端窗口中软包装,但按字符而不是按单词。如何按单词将输出硬包装到字符的特定宽度?
我无法手动将中断添加到 $questionText
,因为包含的变量可以是任意长度。
我的搜索都将我引导至 fmt
和 fold
,但那些需要文本文件作为输入,而不是变量。
我正在寻找的是类似于 echo
的东西,但可以选择将输出自动换行到指定的宽度。
最佳答案
My searches all lead me to
fmt
andfold
, but those want text files as input, not variables.
只需使用管道:
printf '%s\n' "$questionText" | fold
使用 -s
选项将使 fold
只在空白字符上换行,而不是在单词中间:
printf '%s\n' "$questionText" | fold -s
关于bash - 如何用硬换行回显字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942370/
我是一名优秀的程序员,十分优秀!