Commands tagged COLUMNS (5)

  • This is super fast and an easy way to test your terminal for 256 color support. Unlike alot of info about changing colors in the terminal, this uses the ncurses termcap/terminfo database to determine the escape codes used to generate the colors for a specific TERM. That means you can switch your terminal and then run this to check the real output. tset xterm-256color at any rate that is some super lean code! Here it is in function form to stick in your .bash_profile aa_256 () { ( x=`tput op` y=`printf %$((${COLUMNS}-6))s`; for i in {0..256}; do o=00$i; echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x; done ) } From my bash_profile: Show Sample Output

    ( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; )
    AskApache · 2010-09-06 10:39:27 4
  • Unlike other methods that use pipes and exec software like tr or sed or subshells, this is an extremely fast way to print a line and will always be able to detect the terminal width or else defaults to 80. It uses bash builtins for printf and echo and works with printf that supports the non-POSIX `-v` option to store result to var instead of printing to stdout. Here it is in a function that lets you change the line character to use and the length with args, it also supports color escape sequences with the echo -e option. function L() { local l=; builtin printf -vl "%${2:-${COLUMNS:-`tput cols 2>&-||echo 80`}}s\n" && echo -e "${l// /${1:-=}}"; } With color: L "`tput setaf 3`=" 1. Use printf to store n space chars followed by a newline to an environment variable "l" where n is local environment variable from $COLUMNS if set, else it will use `tput cols` and if that fails it will default to 80. 2. If printf succeeds then echo `$l` that contains the chars, replacing all blank spaces with "-" (can be changed to anything you want). From: Show Sample Output

    printf -vl "%${COLUMNS:-`tput cols 2>&-||echo 80`}s\n" && echo ${l// /-};
    AskApache · 2016-09-25 10:37:20 9
  • Using the --table-truncate ( -T ) option, you can specify the columns you will allow to be truncated. This helps when you have some columns that are unusually long, or a small terminal window. In this example we will print out the /etc/passwd file in columns. We are using a colon as our separator ( -s: ), defining that we want table output ( -t ), defining the column names ( -N ) and allowing the column NAME to be truncated ( -T ). Show Sample Output

    column -s: -t -n . -N USERNAME,PASS,UID,GID,NAME,HOMEDIR,SHELL -T NAME /etc/passwd|sed "1,2 i $(printf %80s|tr ' ' '=')"
    wuseman1 · 2022-08-22 09:29:14 583
  • 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 5
  • Basically, \033[ is a semi-portable unix escape character. It should work in linux, osx, bsd, etc. The first option is 38. This tells whatever is interpreting this (and this is merely convention) that a special color sequence follows. The next option is 5 which says that the next option will specify a color ? {0..256} of course. These options, as you can see, are separated by a single `;` and the entire escape sequence is followed by a mandatory `m`. The second escape sequence (following "COLOR") is simply to clear all terminal attributes (for our purposes, it clears color). This for loop is helpful for testing all 256 colors in a 256 console (note: this will not work in a standard Linux tty console) or to see which number corresponds to which color so that perhaps you can use it! Show Sample Output

    for i in {0..256}; do echo -e "${i} \033[38;05;${i}m COLOR \033[0m"; done
    Benharper · 2015-12-17 23:49:42 8

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

Check These Out

Calculate days on which Friday the 13th occurs
I removed the dependency of the English language

Change a specific value in a path
Awk replaces the value of a specific field while retaining the field separator "/" .

Merge *.pdf files
Merge all pdf files in the directory into one pdf file (the out.pdf file)

Redefine the cd command's behavior
Often, the very next command after the cd command is 'ls', so why not combine them?. Tested on a Red Hat derivative and Mac OS X Leopard Update: changed ${1:-$HOME} to "${@:-$HOME}" to accomodate directories with spaces in the names

Show the PATH, one directory per line (part 2)
Here is another way to show the path, one directory per line. The command `tr` translates the colon into the new line, taking input from the $PATH variable

List directories recursively showing its sizes using only ls and grep
ls -lhR Lists everithing using -l "long listing format" wich includes the space used by the folder. Displays it in -h "human readable form" (i.e. 2.2G, 32K), and -R recurses subfolders. grep -e using a regex, show lines containing the word "total" or a ":" at the end of the line (those with the name of the folder) only.

Find the package that installed a command

Print all git repos from a user
in case you could afford installing jq

Extract all of the files on an RPM on a non-RPM *nix

cd to (or operate on) a file across parallel directories
This is useful for quickly jumping around branches in a file system, or operating on a parellel file. This is tested in bash. cd to (substitute in PWD, a for b) where PWD is the bash environmental variable for the "working directory"

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: