Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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.