I’ve been using Linux since 1999 and would class myself as an intermediate-level BASH user. I write the occasional script in BASH, but tend to use PHP-CLI for my more complex scripting needs (after all, I am a certified PHP developer).
Here are a few pointers for new users who are less familiar with the BASH.
BASH Tip - Custom Command Prompt
The default command prompt for a new terminal window will look something like this:
I find when the current path is quite long and you’re typing in lengthy commands, the terminal display looks cluttered.
So, to improve the layout, create a custom prompt by adding the following lines to your ~/.bashrc file:
# Customize prompt - put directory path and username on separate lines PS1="\n\w\n\u > "
This will display a new line (\n), then the current directory path (\w), then another new line (\n) followed by the user login name (\u).
When you now open a new terminal window, the prompt will look like this:
/path1/path2/path3 loginname > _
Some useful format characters (there are more):
\d - date - format = "Mon May 03" \n - newline \r - carriage return \t - current time - format = 24-hour HH:MM:SS \T - current time - format = 12-hour HH:MM:SS \@ - current time - format = 12-hour with am/pm \u - the login name of the current user \w - the current working directory \W - the basename of the current working directory \! - the history number of this command
There are many more ways to customize the prompt including adding colour and even inserting the output from other commands into the prompt text. Just google “bash ps1″ for further info and ideas.
BASH Tip - Getting Command Help
There are hundreds and thousands of commands available through the shell with many new ones being regularly added. Just knowing which command to use for a particular purpose can be daunting for the beginner. Of course you can google for clues but typing in a command parrot-fashion won’t tell you how to apply it’s various options.
Most commands have a corresponding manual page available from the command line and it’s a good idea to get into the habit of reading the manual page for any command that you’re using for the first time.
Find a Command with ‘apropos’
I have found this oddly named command very useful over the years. Think of a topic and see if any commands are installed in relation to that topic.
> apropos sort apt-sortpkgs (1) - Utility to sort package index files bunzip2 (1) - a block-sorting file compressor, v1.0.4 bzip2 (1) - a block-sorting file compressor, v1.0.4 comm (1) - compare two sorted files line by line ldap_parse_sort_control (3) - Decode the information returned from a search ldap_sort (3) - LDAP sorting routines (deprecated) ldap_sort_entries (3) - LDAP sorting routines (deprecated) ldap_sort_strcasecmp (3) - LDAP sorting routines (deprecated) ldap_sort_values (3) - LDAP sorting routines (deprecated) sort (1) - sort lines of text files tsort (1) - perform topological sort winop (3blt) - Perform assorted window operations XConsortium (7) - X Consortium information
The commands are listed followed by the manual section number and then a short description – it’s important to make a note of the section number as some commands may appear in several sections and have different help information in different sections.
Just for info, the section numbers have the following meaning:
1 - General commands 2 - System calls 3 - C library functions 4 - Special files (usually devices, those found in /dev) and drivers 5 - File formats and conventions 6 - Games and screensavers 7 - Miscellanea 8 - System administration commands and daemons
If a command only appears in a single section, then you can display the manual page with:
> man COMMAND_NAME
If a command appears in several sections then you can differentiate by specifying the manual page section that you’re interested in:
> man SECTION_NUMBER COMMAND_NAME
RTFM (Read the Flipping Manual)
Once you’ve found the command your looking for, consult the manual page for instructions about it’s usage.
> man sort SORT(1) User Commands SORT(1) NAME sort - sort lines of text files SYNOPSIS sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F DESCRIPTION Write sorted concatenation of all FILE(s) to standard output. Mandatory arguments to long options are mandatory for short options too. Ordering options: -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters Manual page sort(1) line 1 Use your ‘page up’ and ‘page down’ keys to navigate through the manual
BASH Tip - Command History
If you’ve used BASH for any length of time, you’ll know that some commands can become rather long and laborious to re-type. The BASH history functionality is a useful-but-not-very-obvious feature that can alleviate the pain and will probably be overlooked by new BASH users. It gives the user the ability to quickly recall, edit and execute previously typed commands.
The simplest form involves pressing the cursor-up and cursor-down to cycle through the list of previous commands until you arrive at the command you are looking for, then pressing ‘ENTER’ to execute it. Unfortunately, if it’s been several days since you typed the command you are looking for, then this process could take some time.
A more direct approach is to type ‘history’ at the prompt and examine the generated list which displays the last several hundred typed commands and a unique index number next to each one. To recall and execute a command simply type ‘!n’ where ‘n’ is the index number.
The list produced by the history command can be several hundred lines long, so to narrow your search, you could filter the history command with a piped grep search:
> history | grep "search string"
A more interactive method of finding previous commands, is to press ‘CTRL + r’ to invoke a ‘reverse-i-search’. In this mode, the user types keywords into an interactive filter (putting a space between each word) and BASH will do it’s best to find the last command containing those keywords. When you’ve found the command you are looking for, press ‘ENTER’ to execute it immediately or ‘ESC’ to edit it first. If you cannot find the command you are looking for, press ‘CTRL + c’ to exit.
There is a lot more to the BASH history feature than I’ve outlined here, but these few tips should help to greatly improve your BASH experience.