Image of the glider from the Game of Life by John Conway
Skip to content

Bash Scripting and Prompts

Bash. The superior Open Source shell. Every other shell comes last when it comes to features, speed and just sheer fun. Yet, there are a couple quirks that if you are not aware of, can get you caught in the crosshairs of frustration.

First, parent and child process releationships. When the shell is launched, a process begins. Anything executed in the shell, be it scripts, programs or executables, are all child processes to the shell. What does this mean? It means, for security reasons, that the execution does not have the ability to alter it's parent process. At least I can't think of any exceptions. So, what happens, for example, when you want to write a script that will put you in another working directory when the script exits? For example, consider the script changeme.sh:

#!/bin/bash
# changeme.sh to create a directory cd into it
cd ~
mkdir changeme
cd changeme

Make the script executable, and run it. Why are you still in the same directory you were before you ran the script? The new directiory 'changeme' exists, but you are not in it. The reason being the script creates a child process to it's parent, namely the shell that you are in. So, although the script is crude, how do you make it work? Easy, make it a function, and put it in a dot file that the Bash schell calls when executed. Now the function will have access to the parent process. Let's see how this works with our same example as above. I will be placing the funciton in my .bashrc file:

#.bashrc
changme2()
{
    cd ~
    mkdir changeme2
    cd changeme2
}

Exit your Bash shell and relaunch it. Now run the 'changeme2' script. Notice not only have you created the new directory 'changeme2', but you are also in it. Again, because you put the function in your .bashrc file, and the file was called when your Bash shell was launched, the function is now part of the parent process and has the ability to make calls to that PID. Easy work around.

Next, the Bash prompt variable PS1 (and PS2, etc.). Hopefully, you are aware that you can modify your Bash prompt to say and look as you please. Custom prompts really show the personalization of your Bash shell. For the longest time, my prompt said 'Yes, Master? '. I've played with differing sayings and such, always having fun with it. However, when I introduced color to my Bash shell, all of the sudden, I began running into a small problem. My text on the command line would began wrapping itself on the same line, thus overwriting what I had just typed. It would happen after so many characters. And it seemed that the more colors I introduced to the prompt, the worse it got. For example, here I am changing my prompt to red, again, in my .bashrc file:

#.bashrc
PS1="\e[1;31m\u@\h \w> \e[0;37m"

For a decent list of PS1 variables and control, see this page.

There, I am making the prompt bold red (\e[1;31m), then changing my typing text back to white (\e[0;37m) through ASCII escape sequences. Okay fine, but begin typing. Notice that your text wraps after about 60 characters or so. Talk about annoying. all because I introduced color to the prompt, my text wraps. Is this a bug, or am I overlooking something?

Well, actually, it isn't really a bug. I opened the sequence for the color using escape sequences, but I never closed the esapes. As such, if I understand it correctly, my typing is falling into the escapes themselves, thus creating a buffer in the PS1 variable. As such, the prompt wraps the text when the buffer overfills. Also, I am getting a little lazy with escaping the color too. Consider the alteration to the exact same prompt below:

#.bashrc
PS1="\[\033[1;31m\]\u@\h \w> \[\033[0;37m\]"

Now, instead of using '\e' to call the escape, I am using '

'. It's a little more cryptic, and harder to read, but it works flawlessly. Now type to your hearts content, and you will notice that your text is not wrapping until it hits the end of the screen. Also, when it wraps, it's on a new line like it should be.

{ 2 } Comments