Bash

Contents

Bash

The shell that is started by default on our host is called bash. When bash is run it displays a command prompt (a.k.a. system prompt or bash prompt) to the user.  This prompt often includes the name of the directory that the user is logged into followed by $.  When the command prompt appears on the screen, the user can enter commands after the prompt and press enter to have the shell execute the command.

Bash’s Hidden Files

If we run the following command we can see all of the files in the current directory including the hidden files.  A hidden file is a file whose name starts with a period.  If we run the command from our home directory we can see the three files: .bash_history, .bash_profile and .bashrc.

$ ls –al

Bash History

When at the command prompt, we can use the up arrow and down arrow keys to display on the command prompt previous commands that we’ve had the shell execute.    These previous commands are stored in .bash_history.

The up arrow and down arrow keys can help speed up the development process. Rather than having to retyping a compile command, for example, we can just use the up arrows to repeat the command to we entered earlier.

The .bashrc and .bash_profile Script Files

Both .bash_profile and .bashrc are script files that are editable by the user.  When we start a terminal application to gain access to a shell prompt, the terminal typically starts a non-login shell.  When starting a non-login shell the script file ~/.bashrc is executed.  When we SSH into a machine, a login shell is started and the scripts in .bash_profile and .bashrc are run.

Some useful things to add in these script files are definitions for environmental variables and aliases.

An environmental variable is a string that can be used to refer to another string (usually much longer) in a script or program.  For example, suppose I often change my current working directory to /home/eric/courses/csci105. I can add an environmental variable named INTRO to .bashrc and use it to refer the path.  In .bashrc  I would add the following.

INTRO=/home/eric/courses/csci105
export INTRO

In order to be able to use the new environmental variable we need to reload the .bashrc file.  To do this you need to either log out and back in or run the following command:  (dot space dot bashrc)

$ . .bashrc

Now when you want to go to your homework directory you can substitute the environmental variable for the homework path.

$ cd $INTRO

Aliases are used to define shortcuts to commands that are invoked repeatedly or are lengthy to type.  Suppose I would like to make it so that when I typed ls the shell executed ls -l.  I can do this by creating an alias for ls -l.

alias ls=’ls –l’

Don’t forget to reload the .bashrc file.  After it has been reloaded, when you type the command ls it will execute ls –l.

© 2019, Eric. All rights reserved.