repeat any string or char n times without spaces between

echo -e ''$_{1..80}'\b+'
(here is character '+' repeated 80 times) Sometimes needed to enhance the title of the script.
Sample Output
$ echo -e ''$_{1..10}'\b,--.,-`-.'
$ echo $x{0..10}"TEST"
$ echo {0..5}" TEST"
echo $x{0..10}"TEST"|tr -d ' '

By: knoppix5
2015-05-05 22:13:33

12 Alternatives + Submit Alt

  • shorter than alternative

    printf "%`tput cols`s"|tr ' ' '#'
    kamathln · 2010-04-05 17:12:35 0
  • Print a row of characters across the terminal. Uses tput to establish the current terminal width, and generates a line of characters just long enough to cross it. In the example '#' is used. It's possible to use a repeating sequence by dividing the columns by the number of characters in the sequence like this: seq -s'~-' 0 $(( $(tput cols) /2 )) | tr -d '[:digit:]' or seq -s'-~?' 0 $(( $(tput cols) /3 )) | tr -d '[:digit:]' You will lose chararacters at the end if the length isn't cleanly divisible. Show Sample Output

    seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
    jgc · 2010-04-01 09:06:44 0
  • Pure Bash This will print a row of characters the width of the screen without using any external executables. In some cases, COLUMNS may not be set. Here is an alternative that uses tput to generate a default if that's the case. And it still avoids using tr. printf -v row "%${COLUMNS:-$(tput cols)}s"; echo ${row// /#} The only disadvantage to either one is that they create a variable. Show Sample Output

    printf -v row "%${COLUMNS}s"; echo ${row// /#}
    dennisw · 2010-04-13 21:56:46 0
  • One of the first functions programmers learn is how to print a line. This is my 100% bash builtin function to do it, which makes it as optimal as a function can be. The COLUMNS environment variable is also set by bash (including bash resetting its value when you resize your term) so its very efficient. I like pretty-output in my shells and have experimented with several ways to output a line the width of the screen using a minimal amount of code. This is like version 9,000 lol. This function is what I use, though when using colors or other terminal features I create separate functions that call this one, since this is the lowest level type of function. It might be better named printl(), but since I use it so much it's more optimal to have the name contain less chars (both for my programming and for the internal workings). If you do use terminal escapes this will reset to default. tput sgr0 For implementation ideas, check my Show Sample Output

    L(){ l=`builtin printf %${2:-$COLUMNS}s` && echo -e "${l// /${1:-=}}"; }
    AskApache · 2010-06-14 04:35:30 3
  • function for .bash_aliases that prints a line of the character of your choice in the color of your choice across the terminal. Default character is "=", default color is white.

    println() {echo -n -e "\e[038;05;${2:-255}m";printf "%$(tput cols)s"|sed "s/ /${1:-=}/g"}
    joedhon · 2011-01-09 18:08:18 0

What Others Think

This looked so promising... Until I realised what was going on. So, all those +'s are plus-space-backspace? Urgh! :-) Not only that, but you're relying on 50 variables not being set. . I tried to mess around with IFS, but according to the bash manual, echo is hardcoded to insert spaces. . I tried a few variants using printf, but that means you need an extra echo. (but it means you're only using builtin commands): printf %s $_{1..50}"="; echo
flatcap · 282 weeks and 1 day ago
My last attempt to get a clean command: echo `yes +|head -50`|tr -d ' '
knoppix5 · 282 weeks and 1 day ago
For a full line of +'s: yes +|head -$COLUMNS|tr -d '\n'
knoppix5 · 282 weeks and 1 day ago
Still don't get how the $_{1..50}"=" works. Care to enlighten? I'd probably go with: ruby -e '$><<($*[0]||"=")*($*[1]||80).to_i+$/' + which can also be used as: ruby -e '$><<($*[0]||"=")*($*[1]||80).to_i+$/' + $COLUMNS or simply w/o params: ruby -e '$><<($*[0]||"=")*($*[1]||80).to_i+$/' ... but that's because I can't do any better. ;)
wejn · 282 weeks and 1 day ago
Ha, thanks to flatcap I get it now. Interesting! . For anyone (also) scratching their head... run: echo $_{1..3}"-" _1=x echo $_{1..3}"-"
wejn · 282 weeks and 1 day ago
@wein: Yeah, it's a pretty crazy shell expansion. _{1..50}"STRING", expands to {_1}STRING<space>${_2}STRING<space> etc It relies on the fact that $_1, $_2, etc don't contain values. It's also vital that the expansion has a string on the end. Without a string the shell only displays $_1.
flatcap · 282 weeks ago
printf %s $_{1..50}"="; echo For this how would I be pass variable? like printf %s $_{1..$i}"="; echo would not work
rakibfiha · 68 weeks ago
printf %s $_{1..50}"="; echo For this how would I be pass variable? like printf %s $_{1..$i}"="; echo would not work
rakibfiha · 68 weeks ago
where as in python it would be much simpler like: numbers = [5, 2, 5, 2, 2] for x in numbers: print('x' * x)
rakibfiha · 68 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: