tput sgr0
For implementation ideas, check my
http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
# When Term Width is 53 Columns Wide (tput cols, $COLUMNS) $ L '*'; L '`' 8; L x 15; L '#' 5; ***************************************************** ```````` xxxxxxxxxxxxxxx # SOLID BRIGHT GREEN LINE (bg and fg are bright green!) $ L "`tput setaf 2;tput rev` " # BLUE BACKGROUND, WHITE text $ L "`tput setaf 7;tput setab 4`-" -----------------------------------------------------
shorter than alternative
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
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
(here is character '+' repeated 80 times) Sometimes needed to enhance the title of the script. Show Sample Output
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.
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.
commandlinefu.com 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.
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.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
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:
L() { for i in $(seq 1 5); do echo -n "="; done; echo; }; set -xv; L
L L + L seq 1 5 ++ seq 1 5 + for i in '$(seq 1 5)' + echo -n = =+ for i in '$(seq 1 5)' + echo -n = =+ for i in '$(seq 1 5)' + echo -n = =+ for i in '$(seq 1 5)' + echo -n = =+ for i in '$(seq 1 5)' + echo -n = =+ echo And Here's how the other one handles 263 characters (I had to cut the results this time to get it to fit, as opposed tohaving to cut the inefficient loop.).. L(){ l=`builtin printf %${2:-$COLUMNS}s` && echo -e "${l// /${1:-=}}"; }; set -xv; L L L + L builtin printf %${2:-$COLUMNS}s ++ builtin printf %236s + l=' ' + echo -e ================================================================================================================================ Using the builtin version of printf is faster than seq, It's very simple compared to the printf's and sprintfs you are probably thinking of. While your command would also be extremelyfast, if you actually measured these 2 functions head to head it would be very obvious which one is optimal. The loop technique you have is awesome, I used to use that all the time before I started using more builtin shell techniques.L() { for i in {1..$COLUMNS}; do echo $i; done;}
And it depends on the output buffer settings of your device, but it would be cheaper to write data to an output device 1 time rather than 80 times. Plus you have to figure that echo isn't magic, passing a function an argument requires that it be parsed, so that's $COLUMNS *1 additional internal loops for echo also. And seq requires even more processing (albeit nanoscopic). So it would be better to:L() { for i in `eval echo {1..$COLUMNS}`; do echo $i; done do echo $i; done;}
L() { local l; for i in $(seq $COLUMNS); do echo -n "="; done; echo; }
The only work that is done by this 2 command function is the printf saves a whole line of chars to a variable, then echo simply writes the value of the variable to the output, the screen, in a single write. The expansion that happens in both is as close to free as you can see in the shell. This is very optimized, and I am a huge geek when it comes to the shell.