Shell Scripting Basics For Newbies!

Before you get started with Shell Scripting, there are some basics that you should know.

If you’re into Linux, the term Shell Scripting would be very familiar to you. However, if you’re a newbie looking to make strides here, the term might be a little confusing to you. While, it’s not very difficult to learn and grasp this awesome resource, beginners might face some initial hiccups before diving in. Let’s just say, Shell script in Linux is a wonderful programming resource that helps save lot of time while also giving you valuable insight into the world of the command-line. 

1. What is Shell Scripting?

-A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Perhaps the biggest advantage of writing a shell script is that the commands and syntax are exactly the same as those directly entered at the command line. The programmer does not have to switch to a totally different syntax, as they would if the script were written in a different language, or if a compiled language was used.

2. Let’s get started

-Before you get started, remember, the command-line itself is a program which is called the shell. Most Linux distros today use Bash.

-You can either work as “administrator” or “superuser”. However, it’s best to test out scripts before putting them to work.

-Deep inside, scripts are just plain text files. Text editors like gedit, emacs, vim etc could be used to write them. However, you must always save them as plain te (not as rich text).

3. Permissions and Names

Scripts are executed like programs, and therefore reuire the proper permissions.

-To execute a script (so anyone can read it):

chmod +x ~/yourfolder/script1

-To execute a script (so only your user can read it):

chmod u+x ~/yourfolder/script1

-To run this script:

cd ~/yourfolder

./script1

-Make note that your script names don’t conflict with commands.

4. Basic Guidelines

-Every script should start with:

#!/bin/bash

-Every new line is a new command. If a command extends over into the next line, it could truncate the previous command giving you an error in the next line. In case, this happens becaise of your text editor, you should turn off text-wrapping. 

-Comment as frequently as possible with a #

-Commands are surrounded by ()

Source: How-To Geek 

 

Leave a Reply