Which shell I am running?
$ echo $SHELL /bin/bash
This command prints the address of the shell you are running.
How do I switch to Bash Shell?
bash
Which shells are installed on my system?
$ cat /etc/shells # /etc/shells: valid login shells /bin/csh /bin/sh /usr/bin/es /usr/bin/ksh /bin/ksh /usr/bin/rc /usr/bin/tcsh /bin/tcsh /usr/bin/esh /bin/dash /bin/bash /bin/rbash
First Example
clear; echo "This months calendar"; cal;
A shell can script can be run on commandline from a file. You can use any text editor which doesn't insert formatting into text to write a shell script. vi and emacs are good editors. Save the following code in file called test.sh:
clear echo "This year's calendar" cal -y
By convention, shell scripts are saved with .sh extension. Change permissions of the file:
chmod 755 test.sh
You can use any one of the following commands to execute your bash script.
bash test.sh sh test.sh ./test.sh
Documenting in code
It is important to get into the habit of documenting the code. Following is a 'hello world' example:
The first line of code beginning with a shebang (#!) is a call to the bash interpreter. It tells the system which interpreter it should used to execute the code. All lines starting with a hash (#) symbol are comments. The last line of code prints the string hello world. %s means string and it is referring to the string "hello world". The newline character (\n) specifies end of line. Anything typed after it would be printed on the next line. printf in BASH is like printf in C.#!/bin/bash #: Title : hello world #: Date : 2010-09-22 #: Author : "Nazim Rahman" #: Version : 1.0 #: Description : print hello world #: Options : None printf "%s\n" "hello world"
- %s is for string
- %d is for integers
- %f is for floating point numbers
- %e is for exponential