gpt4 book ai didi

bash - 如何用硬换行回显字符串?

转载 作者:行者123 更新时间:2023-11-29 09:35:06 27 4
gpt4 key购买 nike

我正在编写一个非常简单的 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,因为包含的变量可以是任意长度。

我的搜索都将我引导至 fmtfold,但那些需要文本文件作为输入,而不是变量。

我正在寻找的是类似于 echo 的东西,但可以选择将输出自动换行到指定的宽度。

最佳答案

My searches all lead me to fmt and fold, 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com