This site is currently
Under Construction
...soon to be...

OpenBSD Bash Shell

Last updated: 2009-06-31

Bash is a better commandline shell.

===

To Install bash-3.2.48

# cd /usr/ports
# make search key=bash-3.2.48
# cd /usr/ports/shells/bash
# make install
# make clean=depends

===

Setting BASH as your login shell:

# chsh -s bash

===

Create Symbolic Link to Standard Bash Location:

# ln -s /usr/local/bin/bash /bin/bash

===

Customize the Bash Shell Prompt:

Default bash prompt variable:
PS1='\s-\v\$ '

Custom prompt string:
PS1='\u@\h \w\$ '

Which will change the prompt to look like this:
user@systemname /path #

To make the custom prompt permanent for root we must add this variable to the dot.profile config file:
# echo "" >> /root/.profile|echo "export PS1='\u@\h \w\\$ '" >> /root/.profile

Now the custom prompt variable will be executed each time the root user logs in.

To do the same for all new users that are created, we must modify the default dot.profile file in /etc/skel:

# echo "export PS1='\u@\h \w\\$ '" >> /etc/skel/.profile

Non root (UID is not 0) will display like this:
user@systemname /path $

===

Fixing Commandline Editing Issues:

This will allow the Home, End, Ctrl-Left, and Ctrl-Right keys to work properly on the bash commandline.

Create a file called .inputrc in /root:
# vi ~/.inputrc

Copy this into the file and save it:

# this is actually equivalent to "\C-?": delete-char
"\e[3~": delete-char

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\eOC": forward-word
"\eOD": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

# VT
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# kvt
"\e[H":beginning-of-line
"\e[F":end-of-line

# rxvt and konsole (i.e. the KDE-app...)
"\e[7~":beginning-of-line
"\e[8~":end-of-line

Now copy this file into the default dot.profile directory for new users as their default bash edit file:

# cp ~/.inputrc /etc/skel

NOTE: Close your terminal session and log back in. I've found that after the first time you install bash and create the profile file, it's best to log off and back on to initialize the changes. After that you can use the next procedure to restarting the bash shell.

===

Restart bash shell without logging in:

For root:

# . /root/.profile
or
# source /root/.profile

Other users:

# . /home/<username>/.profile
or
# source /home/<username>/.profile

NOTE: The dot and space ". " before the file path is the ksh "source" command. Make sure the dot is the first character in the line and is followed by a space.

===

Bash History:

Commandline history is stored in username root folder file:
/root/.bash_history
or
/home/<username>/.bash_history

Clear history file:
# history -c

Execute a Specific Command from History:

If you want to repeat the command #4, you can do !4 as shown below:

# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release

# !4
cat /etc/redhat-release

Total Number of Lines in the History using HISTSIZE:

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change.
In this example, only 450 command will be stored in the bash history. The file may not exist so you will have to create it.

# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

Or do this:
# export HISTSIZE=450
# export HISTFILESIZE=450

Force History to NOT Remember a Particular Command using HISTCONTROL:

# export HISTCONTROL=ignorespace

Now placing a space before your command on the commandline will cause it not to be logged in the hostory file.

Disable History:

# export HISTSIZE=0

NOTE: If you move a script file and the file was executed from the commandline before it was moved, you may get an error saying the file can't be found. Simply clear the history file to elliminate the annoying error.

===

Sample Bash Script:

#!/bin/bash
# Test if a directory exists.

DIR="$1"

if [ $# -ne 1 ]
then
echo "Usage: $0 {dir-name}"
exit 1
fi

if [ -d "$DIR" ]
then
echo "$DIR directory exists!"
else
echo "$DIR directory not found!"
fi

Exit and Save (:wq)

To make sure that the script is executable:

# chmod 700 <filename>

Test the Script:

# ./testdir /etc
/etc directory exists!

# ./testdir /etc/a
/etc/a directory not found!

===

Shell Doesn't Recognize Newly Installed Binaries:

If your shell doesn't recognize the installed command, flush the cache.

Bash Shell:

# hash -r

CSH Shell:

# rehash

===

References:

Customize your Bash Shell:
http://www.lifeaftercoffee.com/2006/10/31/customize-your-bash-prompt/

===