问题背景
要在终端里打印一条分隔线,这条分隔线由”#“组成,宽度与终端的宽度相同。
解决思路
问题一、如何重复字符串?
#!/bin/sh printf '#%.0s' {1..100}
问题二、如何获取终端的宽度?
#!/bin/sh tput cols
最终的代码
#!/bin/sh printf '#%.0s' $(seq $(tput cols)) # 可能你需要在结尾加上一个换行符,默认printf是不带换行符的
参考文献
WikiNotes/重复字符串
How can I repeat a character
How do I find the width & height of a terminal window?