Facebook Twitter Reset

This Tutorial Has Been Viewed 831 Times.

Shell Scripts Part 1

Here is a simple shell script to start with,
#!/bin/sh
MY_MESSAGE="Hello World"
echo $MY_MESSAGE

This assigns the string “Hello World” to the variable MY_MESSAGE then echoes out the value of the variable.
Note that we need the quotes around the string Hello World. Whereas we could get away with echo Hello World because echo will take any number of parameters, a variable can only hold one value, so a string with spaces must be quoted to that the shell knows to treat it all as one. Otherwise, the shell will try to execute the command World after assigning MY_MESSAGE=Hello

The shell does not care about types of variables; they may store strings, integers, real numbers – anything you like.
If you assign a string to a variable then try to add a number to it, you will not get away with it:

$ x="hello"
$ y=`expr $x + 2`
expr: non-numeric argument
$

Since the external program expr only expects numbers. But there is no syntactic difference between:

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Incoming search terms:




You will also be interested in ,

No comments yet.

Leave a Comment

Spam protection by WP Captcha-Free

Affordable Seo PackagesSeo BlogEdu Backlinks
More in Css, JavaScripts, scripts (53 of 152 articles)