Prompt me, Bash! Pronto!

While searching for a promt command for bash to trimm long pwds (current working directory) I stumbled uppon “Blog.ubrious“. After a little hacking I adopted it to work for linux bash. It also cuts off leading characters when pwd is longer than 20 characters with build-in commands only. Plus I opted to use a ‘@’ as dilimeter between user name and host name so to rather have ssh style. Here is what I came up with:

PROMPT_COMMAND='PS1="\[33[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\33[32m\\]"; else echo "\\[\33[31m\\]"; fi\`[\u'@'\h: \`if [[ ${#PWD} > 20 ]]; then echo ${PWD:$((${#PWD}-20))}; else echo "\\w"; fi\`]\$\[33[0m\] "; echo -ne "33]0;`hostname -s`:`pwd`07"'

If wordpress does mess up the code (which I hope does not happen) please visit the original blogger’s post since he was able to upload a text file (I’m not, unfortunatelly).Here is a small list on what it can do for you and a little demo:

  1. Changes to red if the last command didn’t have a return code of 0
  2. Includes your bash history # for easy history command repeating. Follow the link to find out how to use !!, !$ and !number.
  3. Makes sure path length doesn’t exceed 30 regardless of what the base name is (bit after last fwd slash).

Bash prompt trimming

The only small hassle about it is even though it uses \w it wouldn’t abreveate my home directory as ‘~’ for me.

Note 2008/05/14: If you want to use this promt command with screen, too, make sure to test $TERM for ‘screen’. For example mine reads:

case "$TERM" in

Let Bash do the math: Doing calculations using that bash

Have you ever wanted to “just do some math” without much fuss? I noticed a while ago (but haven’t really used it since) that bash (born again shell) has, amongst many other evaluations, an arithmetic one. Using the syntax $((expression)) you can evaluate the expression to arithmetically. Operations allowed are quite a few. From man bash:

The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, asso ciativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.

       id++ id--   variable post-increment and post-decrement
       ++id --id   variable pre-increment and pre-decrement
       - +             unary minus and plus
       ! ~             logical and bitwise negation
       **              exponentiation
       * / %         multiplication, division, remainder
       + -             addition, subtraction
       <>       left and right bitwise shifts
       == !=       equality and inequality
       &               bitwise AND
       ^               bitwise exclusive OR
       |                bitwise OR
       &&             logical AND
       ||              logical OR
                        conditional operator
       = *= /= %= += -= <>= &= ^= |=
       expr1 , expr2

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is ref erenced, or when a variable which has been given the integer attribute using declare -i is assigned a value. A null value evaluates to 0. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Other wise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arith metic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.