Shell Script Tutorial#3 Arguments
July 11th, 2005

So, far we have learned the very basic about shell script. i) What we script is what we were to type. And, ii) how to use variables in shell script. In this chapter, the subject is using arguments. It will rely heavily on the use of variable. So, I’ll do a little review for you.
- a=”Hello” will assign value “Hello” (with out quotes) to the variable a. Note that there is no space before and after the equal sign
- echo $a; shell will intepret this command by substuting the value of variable a into the command. So, in this context, it’s equivalent to echo Hello
Lets imgine we want to extend our hello world script so that it takes name as the argument and the shell script will echo “Hello, xxx” where xxx is the name you entered as the argument. That is we want a program that does the following job
$./hello.sh Tee
Hello, Tee
$./hello.sh World
Hello, World
$./hello.sh Tee
Hello, Tee
$./hello.sh World
Hello, World
Lets take this idea to practical by create a file that contains the code below (hello.sh)
#!/bin/sh
theName=$1
echo "Hello, $theName"
#!/bin/sh
theName=$1
echo "Hello, $theName"
Changing the permisssion using chmod u+x, and try executing it using the following command
$./hello.sh Tee
Hello, Tee
$./hello.sh World
Hello, World
$./hello.sh Tee
Hello, Tee
$./hello.sh World
Hello, World
So, it works. Let me explain the code we just did. Ignoring the first line, the second line reads theName=$1. This line assign the value of variable 1 to variable theName. $1 is a very special variable it refers to the first argument ($0 refers to the script’s name it self; hello.sh, in this case).
Now, lets suppose we want to write a program that takes two arguments. You might be able to guess what to do. Yes, I know you are right. To refer to the second arguement, you want to use $2. This is a little example that will teach you. (hello2.sh)
#!/bin/sh
name1=$1
name2=$2
echo "Hello, $1 and $2"
#!/bin/sh
name1=$1
name2=$2
echo "Hello, $1 and $2"
So, if you change the permission and run it, you will find
$./hello2.sh Thing1 Thing2
Hello, Thing1 and Thing2
$./hello2.sh Thing1 Thing2
Hello, Thing1 and Thing2
So, you just have learned that $1 refers to the first argument, $2 refers to the second arguement, $3 to the third and so on. And remember $0 refer to the script name itself.
We know how to use arguments. Now, lets learn how to count the number of arguments. The magic word is $#. Suppose we want to write a program that counts number of argument and return it. I know it’s kinda silly but it’s for educational purpose. We writes (argCount.sh)
numArgs=$#
echo "number of argument: $numArgs"
numArgs=$#
echo "number of argument: $numArgs"
If we run this code we will find
$./argCount.sh
number of argument: 0
$./argCount.sh one two three four five
number of arguement: 5
$./argCount.sh
number of argument: 0
$./argCount.sh one two three four five
number of arguement: 5
In summary, in this chapter you learned how to use arguements and how to count the number of arguments. Here is a little list for you
- $1 refers to the first argument. $2 refers to the second argument. $3 refers to the third .. and so on
- $# is a special varible that represents that number of arguments.
In the next chapter, I’ll tell you how to tell shell script to make decision. Have fun scripting
Entry Filed under: Shell Script Tutorial
Leave a Comment
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