Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Unix shell scripting starter guide

Author: Daniel Bailey
by Daniel Bailey
Posted: Oct 19, 2015

Introduction

The first script we are going to use will be a very simple script that will output a text "Hello Unix". I recommend that you use vi to create and edit the file. Run the following command:

vi hello.sh

If you do not know how to use the vi, I would recommend that you read a quick guide online. Now, enter the following code in the file:

  • bin/sh
  • This is my first script.

echo "Hello Unix"

Save the file and then close it by clicking Escape then followed by ":wq" and then Return. The first line of the file will tell unix which shell it's going to use to execute your file. /bin/sh is a default location for the bourne shell. Linux will normally point to the bourne again shell - a remake of the original unix shell but works pretty much almost the same. The 2nd line of your file is just a simple comment. The comments are usually ignored by the shell interpreter but are useful when developing complex and large and scripts. Most people forget what their original intention or logic was when coding a certain script and it's far much easier to read the comment you left than it is to try and understand complex and large sections of code. Before we run this script, we must first make the script executable. We will use the unix chmod command to do this.

chmod u+x hello.sh

The script is now executable. This command tells unix to set the x flag for the user level access of the file. We are now able to run the file.

If you do not have "." in the unix PATH environment variable, you will need to proceed the name of your script with "./" so as to execute it. It is considered to be a risk to put "." in your PATH environment variable, thus we are going to assume that you do not have it. Now you can go ahead and execute your script by using the command below.

./hello.sh

You will now see the text "Hello Unix" output, congrats, you now have created your very first unix script!

Variables

Variables are very important parts of any script or program. A variable is a very simple way to refer to a block of data in memory that can be modified. Variables in a unix script can be assigned just any type of value, such as a number or a text string. In unix, we simply put in;

VARIABLE_NAME=value

to create a variable.

Note; We don't have to put the variable name in the uppercase, but this is the standard way of naming the variables in unix shell scripting. The text "VARIABLE_NAME" can actually be anything you want, as long as it only contains letters, numbers, and/or an underscore. Variable names also cannot start with a number. Immediately after the equal sign you input the value, there must be no space whatsoever between the equal sign and the variable name. To use the variables, we just simply put a dollar sign "$" right before the name of the variable we are using in our script code.

Source : Click Here

About the Author

Author is an expert article writer who has written many articles related to C Programming Guide. Currently, he is writing content on C Programming Reference Sheet .

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Daniel Bailey

Daniel Bailey

Member since: Jul 23, 2015
Published articles: 5

Related Articles