Bash: Your forgotten friend (part 1)
August 14th, 2008
In the name of meta-productivity, or improving productivity by improving process, I’ve come to learn and love Bash. For those who aren’t familiar, Bash is the “scary” black terminal in Mac and Linux. Its often recognized as the home of true geeks a la Wargames<>. From here out, I’ll assume a basic knowledge of bash (what export and similar commands do). In an effort to spread some of the joy of Bash, I’ve included a few handy features and tricks that help me out day to day. <!—more—> The Prompt
The prompt is one of the first customizations that many people make. The default prompt isn’t informative. You’re given no information of where you are in the filesystem (ie your directory structure) or a clear idea of which user you’re operating under.
The code to make this happen is segmented for readability and easy updating (I’m a fickle user, what can I say?).
<img class=”aligncenter” src=”http://justinlilly.com/static/uploads/2008/08/prompt.png” alt=”prompt.png” width=”300” height=”100” /> <pre class=”prettyprint lang-bash”><code># Prompt Colors BGREEN=’[033[1;32m]’ GREEN=’[033[0;32m]’ BRED=’[033[1;31m]’ RED=’[033[0;31m]’ BBLUE=’[033[1;34m]’ BLUE=’[033[0;34m]’ NORMAL=’[033[00m]’ PS1=”${BLUE}(${NORMAL}w${BLUE}) ${NORMAL}u${BLUE}@h${RED}$ ${NORMAL}”</code></pre> A bit of translation, the weird character combinations are what it takes to tell your terminal (the black window) how to make colors. the w’s and such, are the values you wish to show in your prompt. A complete list of these values can be found in your bash man pages or here<>, as the bash man page is rather long (but informative!).
The Output
Navigating around your filesystem with screenfuls of grey text is a quick way to get a headache. If you want to get work done, however, adding a bit of color coding can make things much easier. Bash takes this into account with the LSCOLORS environment variable. Represented in foreground/background color pairs, there are 11 values (for a total of 22 characters) which make up how your terminal outputs the color.
The designations for color and the order they go in can be seen in the man page for ls or here<>. With an LSCOLORS variable of ‘Gxfxcxdxdxegedabagacad’, we can go from drab and grey, to informative and colorful.
<img src=”http://justinlilly.com/static/uploads/2008/08/ls-colors.png” alt=”ls_colors.png” width=”416” height=”175” />
So our .bash_profile file can be seen below or you can download it here<>.
<code class=”prettyprint lang-bash”># Better looking ls output export LSCOLORS=’Gxfxcxdxdxegedabagacad’</code>
<code class=”prettyprint lang-bash”># Prompt Colors</code>
BGREEN=’[033[1;32m]’
GREEN=’[033[0;32m]’
BRED=’[033[1;31m]’
RED=’[033[0;31m]’
BBLUE=’[033[1;34m]’
BLUE=’[033[0;34m]’
NORMAL=’[033[00m]’
# Prompt definition PS1=”${BLUE}(${NORMAL}w${BLUE}) ${NORMAL}u${BLUE}@h${RED}$ ${NORMAL}”
In <a href=”http://justinlilly.com/2008/08/20/bash-your-forgotten-friend-part-2/“>part 2</a> of the series, I’ll walk though some of the alias I have setup which make life easier.
Comments are disabled. If that bothers you, please contact me on twitter at @justinlilly and let me know.