UNIX history
In this series of posts I shall try to outline some of the most important items of BASH SHELL programming and provide examples and exercises. I have been teaching UNIX & BASH shell scripting in the Academia since 2000. Throughout these years I have expanded my experience and knowledge on UNIX & BASH shell scripting and thus I am able to elaborate numerous examples on several shell programming aspects.
Nowadays, it is a common knowledge that UNIX goes back to the late 60s and ATT where a team of bright engineers developed a multiuser , multitasking network capable secure Operating System. The initial version of UNIX was coded using the PDP-7 machine language but in the early 70s after the introduction of the C language it was recoded and ported to several other machines. Linux is a UNIX compatible OS initially developed by Linus Torvalds. Since that time numerous new Linux distributions have emerged based on the original Linux kernel. Some of them are commercial while others are commuted to the idea of open source free software.
The powerful feature of UNIX is the console and the shell. Of course one would argue that the GUI is valuable for everyday use. I could not agree more. The GUI is the tool for workstations but for the case of a server a GUI will just consume resources and nobody will ever see the fancy windows and colors. Additionally, the scripting capabilities of the shell give the ability to automate management and maintenance tasks.
The BASH SHELL
Simply put, the SHELL is a piece of software that realizes an interface between the user and the Operating System (the kernel). In a GUI the shell starts by starting the "konsole" or "terminal" application etc. Without a GUI the SHELL is the black screen that appears after login (the same stands for SSH). The user sees a '$' or '#' and a cursor waiting for the user to type a command.
The user may type a command or the name of a script and press [ENTER]. Then the command is interpreted by the shell and passed to the OS. The OS performs the required actions and responds (or not) with the command output or an error message.
The SHELL ecosystem includes several variations of the original "standard shell". To name a few there is "korn shell" ( ksh), Z shell, zsh, C shell (csh or tcsh) and Bourne shell (bash). The difference between them are mainly the programming features, the command syntax and start-files. The bash shell was written by Brian Fox for the GNU Project.
The first commands - Navigating
While describing the syntax and functionality of the shell commands the [ ] will be using ed to notify that the item included is optional (the [ ] are not used while typing the command ) and < > to notify that something is obligatory. The general command syntax is:
<command name> [-options] [arguments]
The options are usually letters or numbers that define the behavior the specific command. They are optional to use and they are distinguished by the '-' sign. The options may be grouped in any possible way. For example the commands ls -l -a, ls -a -l, ls -al and ls -la have exactly the same result and output. The arguments are the entities (files, directories, users, data etc) that the command uses as input or acts upon. They are optional too. The options and arguments are separated by space.
pwd - print working directory. The command returns the current full path.
cd - change the shell working directory. cd [-L] [-P] [dir]. Change the current directory to dir. The default DIR is the value of the user's HOME directory, which is stored in the HOME shell variable.
Options:
-L force symbolic links to be followed.
-P use the physical directory structure without following symbolic links.
ls - list directory contents. ls [OPTION]... [FILE]... The ls command may have a lot of options and behave accordingly. For more info, the user my refer to the relevant man page or the unix.com page
Options:
-a, --all do not ignore entries starting with .
-l, use a long listing formatCheck
-s, --size with -l, print size of each file, in blocks
-i, --inode with -l, print the index number of each file
-d, --directory list directory entries instead of contents, and do not dereference symbolic links
One important piece of information to keep in mind is the output format i.e. the names of the columns. This information may be quite useful when developing filters and scripts. For example in the commonly used form ls -al the first column describes the mode of the file, the second the number of the hard links for this file, the third and fourth the user and the group that own the file and then there may be the creation date or last touch date, the size and the name of the file.