- Shell Script are interpreted and not compiled
- .sh for the shell file is not mandatory
- VS Code for Shell Scripting
Following are the types of Shell on a system :
- sh – born shell
- bash – born again shell (improved version of sh)
Sample Program to print Hello World :
#! /bin/bash echo "Hello World.....Welcome to Shell Script "
File : HelloWorld.sh
- The first line is the location of the bash which has to be used to execute, you can find the location of bash using command “cat /etc/shells”
- #! is called shebang. It is also called sha-bang, hashbang, pound-bang, or hash-pling.
- When a text file with a shebang is used as if it is an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file’s initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data. For example, if a script is named with the path path/to/script, and it starts with the following line,
#!/bin/sh, then the program loader is instructed to run the program /bin/sh, passing path/to/script as the first argument. In Linux, this behaviour is the result of both kernel and user-space code.
Some typical shebang lines:
#!/bin/sh– Execute the file using the Bourne shell, or a compatible shell, assumed to be in the /bin directory
#!/bin/bash– Execute the file using the Bash shell
#!/usr/bin/env python3– Execute with a Python interpreter, using the program search path to find it
#!/bin/false– Do nothing, but return a non-zero exit status, indicating failure. Used to prevent stand-alone execution of a script file intended for execution in a specific context, such as by the
.command from sh/bash,
sourcefrom csh/tcsh, or as a .profile, .cshrc, or .login file.
Now how do you execute the file :
1. cd to the directory where the file is located
2. Then ./HelloWorld.sh
3. Now you need to give execute permission to the sh file before executing because by default linux does not give execute permission to a file. (Courtesy of umask)