Bash – Introduction and configuration files

In this article on the shell Bash, we will try to explain in a simple way how to configure, operate and program in Bash. There are other command interpreters that are fully functional and powerful, but we will focus on Bash as it is the most used.

Every UNIX system administrator in general and Linux in particular, should learn a minimum of programming tasks in Bash so that he can automate and manage tasks and jobs in the system. There are many options, and once you take a liking to this programming language, you can not imagine a day as administrator without using these techniques.

Programming in Bash is easy (although it can be tough at the beginning for some people) and uses are unmeasurable. Like with many other aspects of learning, it mostly a matter of trial and error, sometimes making the question: “what would happen if I change this?”.

INTRODUCTION

 

A command line interpreter is a program that works as an interface to the operating system. It handles translation of user commands into instructions that the operating system can understand and vice versa, translate the result returned by the operating system into a language that users can understand.

Bash was written by the GNU Project and belongs to the user interface or text-mode category.

This article assumes that you already have Bash installed on your system (by default most distributions install and configure Bash shell prompt as the system command line interpreter). To use Bash you have to use the system in text mode or open a command line console in graphical mode.

HOW TO CONFIGURE THE COMMAND LINE INTERPRETER

The distribution we use configures Bash automatically, so we can start using it immediately. But if we change or add additional configuration parameters, it is good to know how to do it.

There are three files in the user home folder that have a special meaning for the Bash shell. These files allow the user to configure the environment of its account automatically as it enters the system, when it starts a subshell or executes commands when it exits the system.

The names of these files are .bash_profile, .bashrc and .bash_logout. If none of these files exist in the user home folder, /etc /profile is used by the system as the bash configuration file.

bash_profile is the most important of the three. It is read and the commands contained in it, executed, each time the user enters the system. Any change in this file will have no effect until you exit and enter the system again. An alternative so that you do not have to leave the system is to run this command: source bash_source.

Bash allows two synonyms for this file .bash_login (derived from the C shell) and .profile (derived from the Bourne and Korn shell). If .bash_profile does not exist, the system first tries with .bash_login and then .profile. Only one of these files is read, even though they exist simultaneously.

 

# .bash_profile example

 

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

 

# User specific environment and startup programs

 

BASH_ENV=$HOME/.bashrc

USERNAME=””

PATH=$PATH:/usr/local/pgsql/bin

MANPATH=$MANPATH:/usr/local/pgsql/man

PGLIB=/usr/local/pgsql/lib

PGDATA=/usr/local/pgsql/data

 

export USERNAME BASH_ENV PATH MANPATH PGLIB PGDATA

 

.bashrc is read when the user starts a subshell, writing for example bash in the command line. This allows us to run different commands for entering the system or for the execution of a subshell. If the user needs the same commands to enter the system and executing subshells, we can include the following line in .bash_profile:

    source .bashrc

# .bashrc example

 

# User specific aliases and functions

 

alias ll=”ls -l –color”

alias lal=”ls -la –color”

alias faq=”cd /home/tom/THE_CORNER/FAQ/”

alias php=”cd /home/tom/THE_CORNER/PHP/”

 

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

.bash_logout is the file read by Bash when we leave the system. We can make a script, for example to delete the temporary files created in our last session or to register the amount of time we have been using the system. If .bash_logout does not exist, no command will be executed when leaving the system.

   # .bash_logout example

 

clear

 

Recommended reading:


Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post
 |  TrackBack URI for this post


Leave a Reply