4.20.2015

Shell scripting basics








1. A shell script is a text file that typically begins with a shebang, as follows:

#!/bin/bash

Shebang is a line on which #! is prefixed to the interpreter path. /bin/bash is the
interpreter command path for Bash.


2. Execution of scripts :

$ bash script.sh # Assuming script is in the current directory.

Or:

$ bash /home/path/script.sh # Using full path of script.sh.


3.Need to give permissions for execution

$ chmod a+x script.sh

4. In Bash, each commands are delimited by using a semicolon or a new line.

$ command1 ; command2

This is equivalent to:
$ command1
$ command2

5. Printing to terminal

$ echo "Welcome to Mindlevels"
Welcome to Mindlevels

6. color codes in shell

foreground

 reset = 0, black = 30, red = 31,green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37.

background

reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44,
magenta = 45, cyan = 46, and white=47


echo -e "\e[1;32m This is green text \e[0m"

Here, \e[1;32m is the escape string that sets the color to red and \e[0m resets the color
back. Replace 32 with the required color code.

7. Identifying the current shell

To identify the shell which is currently being used.

echo $SHELL
Or:
echo $0