5 bash snippets

Toggle line-wrapping in the terminal with 'tput rmam' and 'tput sram'

The command:

tput rmam

will disable line wrapping so that long lines are truncated to width of the terminal ($COLUMNS).

The command:

tput smam

will re-enable it.

This seems to known as "automatic margin" mode, hence smam is enter_am_mode and rmam is exit_am_mode.

Some terminals may not support this functionality.

Tagged bash and linux.


Use 'less -S' for horizontal scrolling

The flag -S (or --chop-long-lines) will cause less to truncate lines at the screen (terminal) boundary, rather than wrapping as it does by default. You can then scroll horizontally (with the arrow keys, for example) to view the full lines when needed.

cat some_file_with_very_long_lines | less -S
Tagged bash, linux, one-liner and cli.


How to right-align text in your bash prompt

Right aligning text by padding with spaces

To have text in your bash prompt ($PS1) hug the right side of the terminal:

PS1="`printf "%${COLUMNS}s\n" "${TEXT}"`$PS1"

(This assumes you want the right-aligned text to appear before the rest of your prompt, if any. Move the $PS1 bit to the left side of the string to have the right-aligned text appear after the rest of your prompt.)

The ${COLUMNS} variable contains the number of columns in the current terminal (it should change if you resize the terminal). The ${TEXT} variable is a placeholder for the text you want to right-align.

The trick here is to use printf to left-pad the string to given width. printf "%ns" "text" will left-pad the given string (here, text) with spaces until the entire string is n characters wide.

Right aligning text by padding with something other than space.

Say you want to pad with - instead of space. Try:

PS1="`printf -vch "%${COLUMNS}s" "${TEXT}"; printf "%s" "${ch// /-}"`$PS1"

This will left-pad the ${TEXT} with spaces, as above, and then replace any spaces with -.

If you have any spaces in ${TEXT} you want to preserve, one hacky work-around is to mark spaces in $TEXT with some other character, say _, and then replace _ with a space () after the other substitution:

$ PS1="$PS1`printf -vch "%${COLUMNS}s" "${TEXT}"; printf -vch "%s" "${ch// /-}"; printf "%s\n" "${ch//_/ }"`"

Drawing a line to the end of the line

I recently added a line containing the date and time to my bash prompt (so I can tell when a given command completed) and wanted to draw a line across the rest of the screen to make it visually easier to tell where a new prompt is displayed. Something like this:

-- Tue 02-Oct-2012 05:19 PM --------------------------------

(Assuming the terminal is 60 characters wide.)

Here's how I did it.

Within my $PROMPT_COMMAND I execute the following:

line="`printf -vch "%${COLUMNS}s" ""; printf "%s" "${ch// /-}"`"
dts="`date +"-- %a %d-%b-%Y %I:%M %p "`"

The first line creates a variable ($line) with ${COLUMNS} dashes (-). This line would span the length of the terminal.

The second line creates a variable ($dts) with my date and time format of choice (prefixed with -- just for kicks).

The ${dts}${line:${#dts}} bit in the third line displays my date and time string ($dts) and then a substring of $line, starting at the length of my date and time string (${#dts}). (In this particular case ${dts} is always exactly 28 characters long, so that value could be hard-coded but this way it works in the general case too.)

If you are curious, the \e[1m\e[32m bit makes the text bold (\e[1m) and green (\e[32m).

Tagged bash, linux and cli.


Append to ~/.bash_history "immediately"

Bash normally waits until a session (terminal) is closed before it writes commands to the history.

You can add a call to history -a to PROMPT_COMMAND to make bash to append your history to ~/.bash_history every time it displays your prompt.


The environment variable PROMPT_COMMAND is executed when bash is about to display your prompt.

The command history -a appends the current history to ~/.bash_history.

Tagged linux and bash.


Create a simple histogram or bar-chart in bash

The following script accepts (via stdin) input like this:

foo 10
bar 21
xyzzy 12

and generates (to stdout) a bar-chart like this:

|   foo | 10 | ##########
|   bar | 21 | #####################
| xyzzy | 12 | ############

where the width of the headings (category and count) is automatically determined by the input values and the width of the bar chart (the number of # characters) is automatically scaled to fit within the current terminal width.

It also demonstrates a few intermediate level awk features such as passing variables (via the -v parameter) and variable-width printf statements (via %*s).

Published 28 Feb 2018
Tagged linux, bash, data, text and awk.


This page was generated at 4:16 PM on 26 Feb 2018.
Copyright © 1999 - 2018 Rodney Waldhoff.