我正在尝试在 bash 中创建一个表 - 我已将所有标题收集到一个 var 中 - 但是当我打印这些包含在 "'s 中的字符串时,这些字符串被视为新列,而不是 printf 命令
当我直接传递相同的值时,它工作正常,但是当我使用 $var 时,我得到了不同的行为。
$ printf '%-20s' "some spaced words" other values; echo -e "\n"
some spaced words other values
$ values='"some spaced words" other values'
$ echo $values
"some spaced words" other values
$ printf '%-20s' $values; echo -e "\n"
"some spaced words" other values
Run Code Online (Sandbox Code Playgroud)
我显然误解了我的变量如何不同< some string >
,因此导致不同的行为。
使用变量时如何获得与输出 1 匹配的输出?
编辑:我可以看到 var 版本有引号 - 有没有办法将带引号的字符串作为变量传递给 printf ?
变量在shell 运行任何命令之前展开,即printf
这里,在变量展开之后进行分词。使用数组而不是标量变量:
values=('some spaced words' other values)
printf %-20s "${values[@]}"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
149 次 |
最近记录: |