shell function to underline a given string.

underline() { echo $1; for (( i=0; $i<${#1}; i=$i+1)); do printf "${2:-=}"; done; printf "\n"; }
underline() will print $1, followed by a series of '=' characters the width of $1. An optional second argument can be used to replace '=' with a given character. This function is useful for breaking lots of data emitted in a for loop into sections which are easier to parse visually. Let's say that 'xxxx' is a very common pattern occurring in a group of CSV files. You could run grep xxxx *.csv This would print the name of each csv file before each matching line, but the output would be hard to parse visually. for i in *.csv; do printf "\n"; underline $i; grep "xxxx" $i; done Will break the output into sections separated by the name of the file, underlined.
Sample Output
$ underline 'Underline me!'
Underline me!
=============

$ underline 'Underline me with dashes!' '-'
Underline me with dashes!
-------------------------

1
By: bartonski
2010-02-26 05:46:49

What Others Think

underline() { echo $1; echo "${1//?/${2:-=}}";} or underline() { echo -e "$1\n${1//?/${2:-=}}";}
mitry · 603 weeks and 2 days ago
Nice. I actually thought of this, but couldn't get the substitution to work (now that I re-read the man page, I see that I missed 'If pattern begins with /, all matches of pattern are replaced with string'. I got flummoxed when I found that bash wasn't using a regex match, and stopped reading the man page carefully). I thought I might be able to shave off an extra character by using underline() { printf "$1\n${1//?/${2:-=}}";} but this doesn't put a newline after the underline... ah. I see... that's 'echo -e' which interprets '\n' not 'echo -n' which removes the trailing new line. Thus I could use underline() { printf "$1\n${1//?/${2:-=}}\n";} but I'd be a stroke over par.
bartonski · 603 weeks and 2 days ago
or using escape sequences underline() { echo -e "\E[4m$1\E[0m" } you can colour it too then...
quigybo · 603 weeks and 2 days 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?

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.

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: