Shell Scripting basics

 Shell Scripting basics

  • A script is a list of commands that can be interpreted and run by a program called scripting language.
  • Commands can be entered interactively at the command line or listed line by line in a text file.
  • Scripting languages are usually not compiled. They are interpreted at runtime.
  • Scripts are generally slower to run than compiled languages, but they are also much easier and faster to develop.
  • Scripts are widely used to automate processes, such as ETL jobs, file backups and archiving, and general system administration tasks.
  • Scripts can be used for nearly any computational task including, application integration, and plug-in and web application development.
  • A shell script is an executable text file in which the first line usually has the form of an interpreter directive.
  • The interpreter directive is also known as a ‘shebang’ directive, and has the following form: 'pound, bang, interpreter' plus an optional argument.
  • Interpreter is an absolute path to an executable program, and the optional argument is a string representing a single argument.
Shell scripts are scripts that invoke a shell program.
For example:
  • ‘#!/bin/sh’ invokes the Bourne shell or other compatible shell program, from the bin directory.
  • ‘#!/bin/bash’ ‘shebang’ invokes the Bash shell.
  • ‘Shebang’ directives aren't limited to shell programs.

                                       

  • The ‘.sh’ extension is a convention used to indicate that the file is a shell script.
  • Change text file into a bash script by echoing the bash shebang, and appending that echoed text to file using the ‘double greater than symbol,’ which is the Bash ‘output redirection’ operator used for appending output to file.
  • Then use the echo command to print the statement ‘echo Hello World’ and again, redirect that output to your bash script.



  • To make it executable, first, check the current permission settings of your script by using the ‘ls’ command with the ‘-l’ option.
  • The R and W entries indicate that the script is readable and writable, but the lack of an X means it is not executable.
  • The R, W, and X entries apply to three user-based permission groups: the owner, which is you, the group, and all users.
  • Make it executable for all users by invoking the ‘chmod’ command with the 'plus X' option.
  • Checking the permission settings and show that the script is executable for all users, due to the ‘X’ entries for all three per mission groups.



  • ‘./hello_world.sh’ and pressing enter, and the text ‘hello world’ appears on the command line.

Comments