stuff=5
stuff='chocolate truffles'
Don't put any spaces before or after the equal sign, or you'll get an error. And if you want to assign a string that contains spaces, you will need to put quotation marks around the string.
This is a good time to note that there are several distinct ways to use quotations marks in a shell script. Let's look at the differences among single quotation marks, double quotation marks, and the backslash character and then follow up with some examples.
· Single quotation marks, as in the preceding example, will always get you exactly what's inside the quotation marks--any characters that might otherwise have special meaning to the shell (like the dollar sign or the backslash) are treated literally.
· Use double quotation marks when you want to assign a string that contains special characters the shell should act on.
· The backslash is used to escape (treat literally) a single character (such as $ or *) that might otherwise be treated as a special character by the shell.
Now let's look at some examples that show when to use each method of quoting.
howdy='Good Morning $USER !'
echo $howdy
Good Morning $USER !
howdy="Good Morning $USER !"
echo $howdy
Good Morning hermie !
In the first case, the value of the howdy variable would probably not be what you wanted. The single quotation marks caused Bash to not treat $USER as a variable. In the second case, the results look much better. The double quotation marks allowed Bash to substitute the value of $USER, which is set automatically when you log in, in the string.
Here's another example that demonstrates a common error:
costmsg="Price is $5.00"
echo $costmsg
Actual result: Price is .00
We thought enough to quote the string, but the dollar sign tells Bash to use the value in the $5 variable, which is not what we wanted.. We can easily solve the problem by prefixing the dollar sign with a backslash, as shown here:
$ costmsg="Price is \$5.00"
$ echo $costmsg
Actual result: Price is $5.00
Arguments and Other Special Variables
Arguments are the values you pass to a shell script. Each value on the command line after the name of the script will be assigned to the special variables $1, $2, $3, and so on. The name of the currently running script is stored in the $0 variable.
Here are some other special variables you will find useful in script writing:
$# The number of arguments
$* The entire argument string
$? The return code from the last command issued
So let's try some examples working with arguments and other special variables. Create an executable script called testvars containing these lines:
echo "My name is $0"
echo "First arg is: $1"
echo "Second arg is: $2"
echo "I got a total of $# arguments."
echo "The full argument string was: $*"
Now if you run this script, here's what you'll see:
$ ./testvars birds have lips
My name is testvars
First arg is: birds
Second arg is: have
I got a total of 3 arguments.
The full argument string was: birds have lips
Previous Lesson: Executing a Script
Next Lesson: Shell Script Logic
Comments (most recent first)
windsor (07 Aug 2010, 02:48)
I am trying to write a c shell script that takes a string as in pput and
search in the current subdirectories.
In other words, find . exec | grep "text to search for" '{}' \; Called it myfind find . exec | grep $1 '{}' \; >myfind "text to search" does not work This does not work.
Michal (03 Aug 2010, 13:31)
I am very new to the world of Linux and I am very confused with it. I have
to write a script for a class and I have no idea how to start. I have an
idea what commands to use, but putting it together is hard. The script is
supposed to let an administrator know when a user has been accessing the
system without permission. Can someone please help? Thanks
chinku (27 Jul 2010, 16:31)
we generally use expr 2 + 3 to add two numbers can i change something to
simply use it as 2 + 3 to get the output
NOYB (13 Jul 2010, 17:29)
Special Variables, Arrays in FOR Loop
ARRAY=("ONE A Z" "TWO B Y" "THREE C X") # for I in ${ARRAY[*]} # Each Item in the Array is listed. # for I in ${ARRAY[@]} # Each Item in the Array is listed. # for I in "${ARRAY[*]}" # Each String in the Array is Concatenated. for I in "${ARRAY[@]}" # Each String in the Array is listed. do echo "LOOP START:" echo $I echo ${I[*]} echo ${I[@]} echo ${I[0]} LOOP_ARRAY=($I) echo LOOP ARRAY: echo $LOOP_ARRAY # 1st Item of Loop Array echo ${LOOP_ARRAY[0]} # 1st Item of Loop Array echo ${LOOP_ARRAY[1]} # 2nd Item of Loop Array echo ${LOOP_ARRAY[2]} # 3rd Item of Loop Array done
jek (17 May 2010, 19:37)
its not an error i want.. its the code for checking the error of a
script.for example i have a script and i forgot to put an utility beside it
to work.then the script containing the error command will check my other
script that has an error and will prompt me to that.
Bob Rankin (17 May 2010, 06:43)
What kind of errors do you want to find?
jek (16 May 2010, 20:16)
i need a scrip which would determine a file with errors and would notify me
that it has errors
Gyanu (04 May 2010, 06:53)
Hi I am new for shell scripting and would like to learn it properly and I
think this way I can learn fast. so please send me updates on Shell
Scripting thanks
selva (03 May 2010, 02:37)
good one, ive been following most of your tips on scripting. They are
simple and easily uniderstandable...keep up the good work :)
cheers!! |
|
|
![]() |
|
| <Send This Link to a Friend> <Bookmark This Page> | ||