There are three types of shell variables available. They are,
a)Local variables
b)Environment variables
c)Shell variables or Special variables
Local variables:
A local variable is a variable, its value will be visible to the current shell.
Environment variables:
An environment variable is a variable, its value will be visible to all the child shell's or child processes started from the shell.
Shell variables or Special variables:
A shell variable or special variable is a variable set by shell for its necessity.Some of these variables are environment variables whereas others are local variables
Thursday, April 7, 2011
Wednesday, April 6, 2011
How to quote the value of the variables appropriately?
First of all, there are two type of quoting. Each one has its own significance. We will see that through examples.
Types of quoting:
a.) Single quoting
b.) Double quoting
When to use single quote?
If you want the value to be treated as exactly entered. Then, should go for quoting through single quotes. But there are some special characters which will not follow the rule thought it is entered inside single quotes.
Eg:
volcano@volcano-laptop:~/shellscript$ os='sun solaris'
volcano@volcano-laptop:~/shellscript$ echo $os
sun solaris
volcano@volcano-laptop:~/shellscript$ echo 'another operating system is Linux'
another operating system is Linux
When to use double quote?
If you want special characters expanded properly while it is part of the value of variable.Then double quote should be used.
volcano@volcano-laptop:~/shellscript$ logged_user=$USER
volcano@volcano-laptop:~/shellscript$ echo "Logged in user is $logged_user" Logged in user is volcano
Types of quoting:
a.) Single quoting
b.) Double quoting
When to use single quote?
If you want the value to be treated as exactly entered. Then, should go for quoting through single quotes. But there are some special characters which will not follow the rule thought it is entered inside single quotes.
Eg:
volcano@volcano-laptop:~/shellscript$ os='sun solaris'
volcano@volcano-laptop:~/shellscript$ echo $os
sun solaris
volcano@volcano-laptop:~/shellscript$ echo 'another operating system is Linux'
another operating system is Linux
When to use double quote?
If you want special characters expanded properly while it is part of the value of variable.Then double quote should be used.
volcano@volcano-laptop:~/shellscript$ logged_user=$USER
volcano@volcano-laptop:~/shellscript$ echo "Logged in user is $logged_user" Logged in user is volcano
Overview about variables in shell script
Variables meaning and its use are more or less similar like other programming language variables. As usual variables are used to hold values. Like other programming languages, shell script doesn't require you to declare the variables.Value of the variables can be retrieved by placing $ (dollar) symbol in front of the variable.
Rules governing variables:
a. Variables name should start either with character or underscore
b. While assigning the value to a variable, no space should be left between the left and right hand side of "=" operator.
c. While assigning the value to a variable, required quotes should be provided to avoid the error. If value has space in between them, then value should be enclosed in quotes.
eg:
"country" and "same" are variable names.
volcano@volcano-laptop:~/shellscript$ country=India
volcano@volcano-laptop:~/shellscript$ echo $country
India
volcano@volcano-laptop:~/shellscript$ same=$country
volcano@volcano-laptop:~/shellscript$ echo $same
India
volcano@volcano-laptop:~/shellscript$ name=sujay kumar
kumar: command not found
volcano@volcano-laptop:~/shellscript$ echo $name
volcano@volcano-laptop:~/shellscript$ name="Sujay Kumar"
volcano@volcano-laptop:~/shellscript$ echo $name
Sujay Kumar
Example for variables in a shell script:
volcano@volcano-laptop:~/shellscript$ cat vari.sh
#!/bin/bash
#variable declaration, assignment, & retrieval of value
country=India
echo $country
same=$country
echo $same
name="Sujay Kumar"
echo $name
volcano@volcano-laptop:~/shellscript$ ./vari.sh
India
India
Sujay Kumar
volcano@volcano-laptop:~/shellscript$
Rules governing variables:
a. Variables name should start either with character or underscore
b. While assigning the value to a variable, no space should be left between the left and right hand side of "=" operator.
c. While assigning the value to a variable, required quotes should be provided to avoid the error. If value has space in between them, then value should be enclosed in quotes.
eg:
"country" and "same" are variable names.
volcano@volcano-laptop:~/shellscript$ country=India
volcano@volcano-laptop:~/shellscript$ echo $country
India
volcano@volcano-laptop:~/shellscript$ same=$country
volcano@volcano-laptop:~/shellscript$ echo $same
India
volcano@volcano-laptop:~/shellscript$ name=sujay kumar
kumar: command not found
volcano@volcano-laptop:~/shellscript$ echo $name
volcano@volcano-laptop:~/shellscript$ name="Sujay Kumar"
volcano@volcano-laptop:~/shellscript$ echo $name
Sujay Kumar
Example for variables in a shell script:
volcano@volcano-laptop:~/shellscript$ cat vari.sh
#!/bin/bash
#variable declaration, assignment, & retrieval of value
country=India
echo $country
same=$country
echo $same
name="Sujay Kumar"
echo $name
volcano@volcano-laptop:~/shellscript$ ./vari.sh
India
India
Sujay Kumar
volcano@volcano-laptop:~/shellscript$
Subscribe to:
Posts (Atom)