Bash LeftPad字符串,变量内部带有空格 [英] Bash LeftPad String with spaces inside variable

查看:46
本文介绍了Bash LeftPad字符串,变量内部带有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

  CUSTOMERNAME="THIS IS A TEST"
  printf "| %-30s " $CUSTOMERNAME

但是问题在于,值 CUSTOMERNAME 具有一定的空间.输出看起来像这样:

But the problem is, that the value CUSTOMERNAME has some space. The output looks likes this:

 | THIS                           | IS                             | A                              | TEST

但是我想要这样的东西

 | THIS IS A TEST                |

我该怎么办?有什么想法吗?

What can I do? Any ideas?

谢谢,豪克

推荐答案

为防止单词扩展,请对该变量加双引号:

To prevent word expansion, double quote the variable:

printf "| %-30s " "$CUSTOMERNAME"

这篇关于Bash LeftPad字符串,变量内部带有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆