「Sehll」- 重複字符串 @20210202

問題背景

要在終端裏打印一條分隔線,這條分隔線由」#「組成,寬度與終端的寬度相同。ide

解決思路

問題1、如何重複字符串?

#!/bin/sh

printf '#%.0s' {1..100}

問題2、如何獲取終端的寬度?

#!/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?字符串

相關文章
相關標籤/搜索