Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Print a row of characters across the terminal

Terminal - Print a row of characters across the terminal
printf "%`tput cols`s"|tr ' ' '#'
2010-04-05 17:12:35
User: kamathln
Functions: printf tr
17
Print a row of characters across the terminal

shorter than alternative

Alternatives

There are 5 alternatives - vote for the best!

Terminal - Alternatives
seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
2010-04-01 09:06:44
User: jgc
Functions: seq tput tr
Tags: seq tr tput
6

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.

printf -v row "%${COLUMNS}s"; echo ${row// /#}
2010-04-13 21:56:46
User: dennisw
Functions: echo printf
Tags: tr tput printf
4

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.

L(){ l=`builtin printf %${2:-$COLUMNS}s` && echo -e "${l// /${1:-=}}"; }
2

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

http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

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

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.

jot -b '#' -s '' $COLUMNS
2010-04-13 22:03:39
User: dennisw
Tags: tr tput printf
-1

For BSD-based systems, including OS X, that don't have seq.

This version provides a default using tput in case $COLUMNS is not set:

jot -b '#' -s '' ${COLUMNS:-$(tput cols)}

Know a better way?

If you can do better, submit your command here.

Your point of view

You must be signed in to comment.

Related sites and podcasts