Skip to main content

Using ‘shopt’ To Adjust Bash Terminal Number Columns After Resizing Window

This solved a long time frustration I had within PuTTY in that when changing the window size, the terminal columns get all messed up and you get some pretty strange behavior (as seen in this blog post). The solution is to use shopt with the checkwinsize option. This will make sure that your bash terminal will always have the correct number of columns.

shopt -s checkwinsize

After throwing this into my ~/.bashrc file (or ~/.bash_profile, if you prefer), my frustration is gone! Whew!

Check out the man page or this linuxnix.com page for more information.

Use CDPATH to Quickly Change Directories

You can create a shortcut to frequently accessed directories by adding them to the CDPATH environment variable.  So, say I frequently access /var/www/html/.  Instead of typing cd /var/www/html, I can add /var/www/ to CDPATH and then I only have to type cd html.

Open ~/.bashrc (or ~/.bash_profile) and add the following line with your frequently used directories separated with a colon (similar to PATH variable).

export CDPATH=$CDPATH:/var/www/

Here’s an example usage:

dhildreth@hostname:~> export CDPATH=$CDPATH:/var/www/
dhildreth@hostname:~> cd html
/var/www/html
dhildreth@hostname:html>

There’s one caveat to using this that I’ve ran into in the past: if you are working with Makefiles and building c/c++ apps, this can potentially confuse the Makefile script. So, if you suddenly can’t build your project after adding this variable, try removing it.

Handy Terminal Keyboard Shortcuts

Put these into your “Terminal Guru” belt and be more productive!

Cursor Movement Control
Ctrl-a: Move cursor to the start of a line
Ctrl-e: Move cursor to the end of a line
Ctrl-Left/Right: Navigate word by word (may not work in all terminals)

Modify Text
Ctrl-w: Delete the whole word to the left of the cursor
Ctrl-k: Erase to end of line
Ctrl-u: Erase to beginning of line

Scrolling/Buffer Control
Shift-PageUp/PageDown: Scroll through current buffer
Ctrl-s: Pause terminal output (program will keep running)
Ctrl-q: Release terminal output (after being paused)
Ctrl-l: Clears the screen. Use this instead of the clear command.

History
Ctrl-r: Search the history (enter to run the command once found)

Bonus Tip: Use ‘!!’ command to run last command and ‘!com’ to run the last command starting with ‘com’.

Process Control
Ctrl-d: Exit
Ctrl-c: Kill the current process
Ctrl-z: Put the current process in the background (fg will restore it)

Are there any keyboard shortcuts that you can’t live without? Tell us about them in the comments below.

msmtp – a (fairly) simple mail submission program

As an oldtime Unix guy, I’ve always been used to having the BSD mail utility to hand, and a suitably configured mail system, so that I can script jobs to run and email the results back to me. I use mail as a sort of glorified syslog facility. With smaller single board Linux computers we don’t always want to install a full mail setup – resources often tend to be limited. A few years back I discovered msmtp http://msmtp.sourceforge.net/.

This utility is an smtp client that submits a file in standard mail format to a mail server. It can submit plain text email or use TLS/SSL etc. I use a couple of script wrappers to emulate, sort of, sendmail and the sending part of the BSD mail utility.

Of course to use msmtp you need a mail server to which you can submit email for delivery. My home server is my mail server, but you could use your ISP’s smtp server. Another problem is that msmtp just fails if it can’t connect to the mail server – it’s up to you to handle that and do something else with that precious message you can’t mail just now! My sample scripts do not deal with that situation.

This very simple script I call sendmail, and it will need customising for your setup…

#!/bin/sh
#

# set these for your setup...
MailServer=mail.server
Domain=MachineName
From="me@my.domain.com"

exec msmtp --host=$MailServer --domain=$Domain --from=$From $*

This is my simple script to emulate the simple parts of the send functionality in the BSD mail utility. It has many shortcomings, but it has served me well…

#!/bin/sh
#
# A sort of shell replacment for the send functionality of
# the standard "mail" utility.
#
# mail [-s subject] recipient(s)
#

u=`id -un`
d="my.email.domain"
r=""
s=""
v=""

while [ $# -gt 0 ]; do
 p="$1"
 shift
 case "$p" in
 -s)
        s="$1"
        if [ $# -gt 0 ]; then shift ; fi
        ;;
 -v)
        v="-d"
        ;;
 -*)
        echo 1>&2 "Option "$p" not recognised."
        exit 1
        ;;
 *)
        r="$r , $p"
        ;;
 esac
done
if [ "$r" = "" ]; then
 echo 1>&2 "No Recipients."
 exit 1
fi
if [ "$s" = "" ]; then
 printf "Subject: "
 read s
fi

s="Subject: $s n"

(echo -e "From: ${u}@$d nTo: $r n$s nn"
 cat ) | sendmail -t $v

So if you have a job to run on the platform, then this will email the output to you…

my_job | mail -s “my_job output” me@my.email.add

msmtp can be loaded from the package systems of most distributions, but I have had occasion to cross-compile the package for installing on a system without package management. I had only limited libraries on my cross compile system, and found that after downloading and extracting the sourcecode from sourceforge, I had to cross compile without some of the advanced features. I used

./configure –build=arm –disable-ssl –disable-gsasl –disable-nls

before doing the make to build the binaries. The resultant binary just submitted plain text email, but that was ok for my use – YMMV. The resultant binary, suitably stripped,is pretty lean.

I recently revisited using msmtp to pre-test a change to my ISP’s new smtp server, before committing the change to my mailserver’s sendmail setup. It can also be useful for testing security settings etc on mail submissions systems.

Jim