它似乎并不那么简单.至少对于我来说.
我需要在printf文本中有一个变量.它是这样的:
FOO="User data" +++++++++++++++++++ $FOO +++++++++++++++++++++
会输出
+++++++++++++++++++ User Data +++++++++++++++++++++
但
FOO="Fooooooo barrrr" +++++++++++++++++++ $FOO +++++++++++++++++++++
应输出
++++++++++++++++ Fooooooo barrrr ++++++++++++++++++
和
FOO="Foooooooooooooooooooo barrrrr" +++++++++++++++++++ $FOO +++++++++++++++++++++
应该
+++++++++ Foooooooooooooooooooo barrrrr +++++++++++
正如你所看到的,我需要一个变量位于n长度线的中间,由+
mark 包围.如何使用printf和其他默认可用命令实现?
(Debian 8)
declare -i x1 x2 x3 width foo="User data" width=50 # total width x2=${#foo}+2 # length of $foo and 2 whitespaces x1=(50-x2)/2 # length of first part x3=$width-x1-x2 # length of last part for ((i=1;i<=$x1;i++)); do echo -n "+"; done echo -n " $foo " for ((i=1;i<=$x3;i++)); do echo -n "+"; done
输出:
+++++++++++++++++++ User data ++++++++++++++++++++
用foo="stackoverflow.com"
:
+++++++++++++++ stackoverflow.com ++++++++++++++++