Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to write a series of the character -, the amount equaling to 50% of the terminal window column size.

I already have the column size. Let's say the terminal column size is 30. My desired end result would be a String variable containing this:

echo $column_width
---------------
# That is 15 x "-"

How should I go about building this variable? Should I loop through 0 < $column_width and concatenate a "-" for each iteration, or is there a smarter way? In case you wonder, this is just some stuff for a bash prompt.

share|improve this question

1 Answer 1

Perl is good at this:

separator=$( perl -e 'print "-" x (qx(tput cols) / 2)' )

With the shell:

separator=$( printf "%*s" $(( $(tput cols) / 2 )) " " | sed 's/ /-/g' )

where tput cols is used to grab the current terminal width.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.