Shell Script Tutorial 2

July 10th, 2005

Hello World, Variables
Last time, we covered the most important basic for shell scripting, that is “what you script is what you type to terminal”+(with some exceptions that you don’t nedd to care). This time we will cover the traditional helloworld program and variables.

Lets first learn how to write a hello world program in shell script, open your favourite text editor and create a new file call helloWorld.sh which contains the following


#!/bin/sh
echo "Hello World"

Save it, and give it a execute permission by using the command chmod u+x helloWorld.sh .Now execute it by using command ./helloWorld.sh


$./helloWorld.sh
Hello World

So, you just have learned two important lessions

  • echo is a command that prints out whatever that follows it to standard output(don’t worry if you don’t know this terminology, I’ll cover it later. For now think of it as printing out to the terminal)
  • The quotes are used to group arguments and it doesn’t get printed. For example, echo Hello World. This is calling command echo with two arguments Hello and World. But, echo “Hello World” is calling echo with one argument that is Hello World. To print the quote we need to use \”

Lets now turn to the subject of variables in script. If you have learned some programming language, this will be a new experience for you. The variables in shell script doesn’t have type. We can say that everything is string. Although there is a trick to assign type to variables in shell script, 99% of the time you won’t have to worry about it. Lets look at the example code below(note that there is no space before and after the equal sign), (var.sh)


#!/bin/sh
myVar=Hello
echo $myVar

When we change the permission and run it, we will find that it prints out


$./var.sh
Hello

Let me explain the code line by line.

  • The first line #!/bin/sh, you have seen this for many time but I’ll explain to you this time what it is. The first line tells when we were to execute it what program should the shell use to execute it.
  • The second line myVar=Hello this is another shell command. Tells the shell to assign value Hello to variable called myVar. The statement myVar=Hello is equivalent to myVar=”Hello”. Since Hello is not a value of a variable it’s instead a string Hello.
  • echo $myVar tells is the command to print the value of myVar which is denoted by $myVar. This statement without the dollar sign, echo myVar will print the text myVar instead of the value of the variable myVar, Hello.

Let’s take this code a little bit further, Now not only we want it to say Hello. We want it to say Hello and your name. For example, Hello Tee. We will modify myVar.sh as follow(note that there is no space before and after the equal sign)


#!/bin/sh
myVar=Hello
myName=Tee
echo "$myVar $myName"

If you run this script you will find that it will say Hello Tee. Let me explain what we just did.

  • The first two lines, like the precursor example, it gives value to variable myVar and myName
  • The echo line is somewhat peculiar if you are not familiar with shell variable. Let me explain to you what it is. Shell will substitue $xxx with the value of variable xxx. So, what this line actually tell the shell is echo “Hello Tee”.

To clarify the idea of variable substitution, the following code will work the same way as the previous example note that I use the \” so that messge will contain quotation marks; otherwise, it’ll only contain Hello Tee not “Hello Tee”


#!/bin/sh
myVar=Hello
myName=Tee
message=\"$myVar $myName\"
echo $message

So, the shell will substitute $myVar and $myName with the value of variables myVar and myName accordingly. So, message will contain “Hello Tee”.

In this chapter we have learned about how to print something to standard output(if you don’t know about this terminology, for now think of it as the terminal), and how to use variable in shell script. Next time, we will learn how to write a shell script that takes arguments.

Entry Filed under: Uncategorized, Shell Script Tutorial

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

July 2005
M T W T F S S
    Aug »
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts