portallinux → bash_galore.htm   This is a windrose

GNU/Linux: Assorted command line tricks and tips

...the shell is awesome!
(by _Mammon, first published at searchlores in April 2008)

Related Sections

da bash cookbookz

Version 0.03: April 2008

General Introduction

Every modern operating system has at least one shell and some have many. Some shells are command-line oriented, such as the GNU/Linux bash shell. Others are graphical, like Windows Explorer or the Macintosh Finder. Some users will interact with the shell only long enough to launch their favorite application, and then never emerge from that until they log off. But most users spend a significant amount of time using the shell. The more you know about your shell(s), the faster and more pro- ductive you can be.
Whether you are a system administrator, a programmer, or an end user, there are certainly occasions where a simple (or perhaps not so simple) shell script can save you time and effort, or facilitate consistency and repeatability for some important task. Even using an alias to change or shorten the name of a command you use often can have a significant effect, as you will be able to constate in _Mammon's .bash_alias.rc script.

See Bash Reference Manual or, for beginners, Machtelt Garrels's Bash Guide for Beginners, both, full texts, on line.

Alternatively, search for
The bash cookbook (OReilly.bash.Cookbook.May.2007.pdf)
Classic Shell Scripting (Classic_Shell_Scripting_OReilly_2005.chm)
Learning The BASH Shell (O'Reilly - Learning The BASH Shell, 3rd Edition_2005.chm)
which have been massively uploaded on the web, and consult them on line.

_Mammon's Introduction

Here are a couple of things that you can add to your ~/.bashrc (which should be probably symlinked to ~/.login) in order to make the CLI prompt more interesting.

bash galore: the functions

Download zipped/bash_setup.tgz and untar.
You'll find inside it:

In the last rc file you'll find, among other things, the following useful search function:
# -------------------------------------------------------------------
# ifind : case insensitive find from the current directory
function ifind () {
        if [ -z "$1" ]
		echo Usage: ifind pattern
		return 1

	find . -iname \*$*\*
# ------------------------------------------------------------------

You will be able to find updated versions of these (and other) files at _Mammon's http://dotfiles.org/~mkfs.

Bash galore: One more

Oh, one more people might be interested in:

# ------------------------------------------------------------------ %<
# alt_wm: run an alternate window manager in a new XServer session.
# Allows two XSessions to be run concurrently. Useful for testing
# new window managers that are buggy, e.g. Beryl or E17.
alt_wm () {
       if [ -z "$1" ]
               echo Usage: alt_wm path [screen]
               return 1

       [ -z "$SCREEN_NUM" ] && SCREEN_NUM="1"

       xinit $1 -- :${SCREEN_NUM}
# ------------------------------------------------------------------ %<
To use, Ctrl-Alt-F1, then "alt_wm `which fluxbox`" assuming you have fluxbox installed.
You should know about backticks (``): see the difference between
echo `date`
echo 'date'
. You can also assign to a variable:

Make sure to type ". ~/.bashrc" if you want the changes to take effect in your current shell session.

There are quite a few more customizations that really make laptops useful.
One of them is to set up a 'battery mode' run level, then edit the ACPI events to switch to that runlevel when the power cord is removed (and to switch back when it is plugged int). The big draw here is that each runlevel specifies shell scripts and programs that are run/started/stopped, so you can stop all unnecessary processes (e.g. CUPS, apache, etc), spin down the hard drive, darken the display, disable wireless, and so on.

Have fun!


Bash comments

The Z shell
~S~ Kane points out that the interactive zsh shell deserves a mention, "as it makes doing most of the crap you need to do with bash more than ten times easier out of the box. Most distros have a seperate package called zsh-completion that pretty much sets it up without any effort".
The z shell has indeed many interesting improvements (like a common command history among all running shells), and its ability to interactively evaluate arguments is indeed dramatically powerful.
Check its documentation.
The choice of shell is one of the most religious choices for GNU/Linux users. If you are new to GNU/Linux just accept (and delve into) the default shell bash, but as time goes and you get more advanced, you many want to change shell because you really miss certain features. This said, I would advise readers to stick to bash until they understand it adeguately, and consider changing shell only in a second phase.

(c) 3rd Millennium: [fravia+], all rights reserved, reversed, revealed and reviled