More precise BASH debugging

Bookmark this category
More precise BASH debugging

$ env PS4=\’ ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) \’ sh -x /etc/profile

* View this command to comment, vote or add to favourites * View all commands by unixmonkey4483

by David Winterbottom (



Modifying the bashrc or bash startup files.

Find the article here.

Copy here:

If you\’ve been learning the command-line and you have the basics down (you should be, as the most effective way to use a computer is a combination of a GUI and command-line), the next step is to customize your environment.

Beginner\’s Tip: \”command-line\” and \”shell\” are often used synonymously. In unix, technically speaking, the shell is what processes the command-line, but usually, they mean the same thing

The ability to fully customize your shell is one of the most powerful things about the command-line. It\’s a dry subject, and mastering it won\’t get you favors from the opposite sex (although it should), but it can be very useful.

There are many ways to customize your shell, but the first one you should learn is modifying your Bash startup files (assuming your shell is Bash, which is the default in OS X, Linux, and many other unices).

When I first learned how to customize bash, I found an overwhelming amount of information and opinion, which made it difficult. This article is intended to give you the fundamental concepts so that you can create your own startup files, and understand how they work. To give you an example, I go through a subset of my own files, section by section.

Let\’s install the example startup files

Beginner\’s Tip: Directory and folder are synonymous. Often folder is used in Windows and OS X and directory is used in Linux, however even Linux represents a directory as a folder graphically

Below are the two example startup files: .bashrc and .bash_profile.

If you would like to use these as your startup files, follow the following directions for your OS.


  1. If you want a backup of your existing files, use the following commands (if the files don\’t already exist, you will get an error. The files will be named .bashrc_ORIGINAL and .bash_profile_ORIGINAL in your home folder):

    cp ~/.bashrc ~/.bashrc_ORIGINAL ; cp ~/.bash_profile ~/.bash_profile_ORIGINAL
  2. Copy .bash_profile and .bashrc to your home folder.
    There are a variety of ways to do this, but the simplest is to use the curl command:

    curl -o ~/.bash#1 \"{rc,_profile}\"
  3. You do not need to log out, just create a new window or tab in iTerm, or a new window in Terminal.

Linux and other unices:

  1. If you want a backup of your existing files, use the following commands (if the files don\’t already exist, you will get an error. The files will be named .bashrc_ORIGINAL and .bash_profile_ORIGINAL in your home folder):

    cp ~/.bashrc ~/.bashrc_ORIGINAL ; cp ~/.bash_profile ~/.bash_profile_ORIGINAL
  2. Copy .bash_profile and .bashrc to your home directory.
    There are a variety of ways to do this, but the simplest is to use the wget (or curl for BSD and others) commands:

    wget -O ~/.bashrc \"\"
    wget -O ~/.bash_profile \"\"


    curl -o ~/.bash#1 \"{rc,_profile}\"
  3. Log out then log back in in order to load .bash_profile. Alternatively, you can do a source ~/.bash_profile to run the files.

What the heck are bash Startup Files?

Beginner\’s Tip: ~ represents your home folder, it is short-hand notation so that you don\’t have to type the whole thing; it is also used when you don\’t know the home folder; for example, my code above works, no matter where your home folder/directory is.

Bash, as well as other unix shells, have files that run when they start. You can modify these files to set preferences, create aliases and functions (a kind of micro-script), and other such fun.

When you start an interactive shell (log into the console, open terminal/xterm/iTerm, or create a new tab in iTerm) the following files are read and run, in this order:

  1. /etc/profile
  2. /etc/bashrc
  3. ~/.bash_profile
  4. ~/.bashrc (Note: only if you call it in .bash_profile or somewhere else)

When an interactive shell, that is not a login shell, is started (when you call \”bash\” from inside a login shell, or open a new tab in Linux) the following files are read and executed, in this order:

  1. /etc/bashrc
  2. ~/.bashrc
Beginner\’s Tip: Normally you can\’t see the . files (files that start with a period) because they are hidden. Depending on your OS, you can simply turn on hidden files. Another option is to open the file in the command-line. Here are a few examples:
In shell: pico .bashrc
In shell: vi .bashrc
In OS X: open .bashrc
In GNOME: gedit .bashrc

/etc/profile and /etc/bashrc are run for all users on the system. Often on your workstation, there is only one user, you. But in systems with more than one user, these files can be used to set generic settings for all users. The files in your home folder, ~/.bashrc and ~/.bash_profile, are only for your particular user (since /etc/bashrc is run before ~/.bashrc, you can override anything in /etc/bashrc by simply setting it again in ~/.bashrc). Normally I only change these, since they are in your home folder, and only you have rights to them, you can change them without worry of affecting anyone else.

When your session starts, these files are run, just as if you typed the commands in yourself. Anything that normally works in the shell works in these files. Since .bash_profile only runs when you first login, you set very little there; the only important thing is your PATH. bashrc is where the meat goes, and will be where you spend all your time.

Continue reading “Modifying the bashrc or bash startup files.”

let a cow tell you your fortune

Here\’s something that I saw on commandlinefu yesterday. That sent me thinking about some command to have the cow file picked randomly 🙂 So, here\’s the original command from the commandlinefu:
let a cow tell you your fortune
$ fortune | <a class="zem_slink freebase/en/cowsay" title="Cowsay" rel="homepage" href="">cowsay</a> -f tux

Let Tux bring the fortune cookie


by David Winterbottom (


Now, for some fun

#!/bin/bash –
#          FILE:
#         USAGE:  ./
#   DESCRIPTION:  Cowsay with random cow file.. 🙂
#       OPTIONS:  —
#          BUGS:  —
#         NOTES:  —
#        AUTHOR:   (),
#       COMPANY:
#       VERSION:  1.0
#       CREATED:  06/01/2010 03:25:59 PM IST
#      REVISION:  —

array=( `ls -1 /usr/share/cowsay/ |tr \’\\n\’ \’ \’` )

fortune |cowsay -f ${array[$random]%%.cow}