IMC!


Contents


Photos

 







Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
03 - Bash variables

 | 

The bash scripts allow for the use of variables. The variable declaration is:

variable_name = value

The scope of this variable is the script where it was created in. To make it available to other programs or scripts, you can write:

export variable_name

To print on the screen the value of a variable you can write (remember to put the dollar sign):

echo $variable_name

To assign the value of a variable to another variable, you can write:

x=$y


Quotes usage
In the bash there are three strings delimiters: the grave accent, the single quotes and the double quotes. Each one gives a different behaviour:
  1. Double quotes: (that is "): visualize a string substituting it with its value.
  2. Single quotes (that is '): don't substitute the value of a variable to its name.
  3. Grave accents (that is `): substitute to the string the result of its execution (useful for example when you want to save the output of a command in a variable).
Let's see an example

#!/bin/bash
x=10

echo "   The variable is: $x" #Prints "   The variable is: 5"
echo '   The variable is: $x' #Prints "   The variable is: $x"
 

Arithmetical operations
To execute arithmetical operations you can use two different syntaxes:
  1. expr included between grave accents (be careful not to use the apostrophe!).
  2. double parenthesis with the dollar sign.
This is an example:

#!/bin/bash
x=5

x=`expr $x + 1`
x=$(($x+1))

echo $x

The allowed mathematical operations are:

SymbolMeaning
+Sum
-Subtraction
*Multiplication
/Division
%Module or remainder

 | 







Comments

No comment present!

Write a comment

You can write here a comment to the article you've just read. Smiles, links and images are not allowed. The maximum comment length is 4000 characters. Please be polite, all the offensive messages will be deleted.

Your comment (lascia bianco!):
Uses (max 25 characters, required)
Web site (max 255 characters, optional)
e-Mail (max 255 characters, optional, will not be published) Your opinion (lascia bianco!):
Comment (max 4000 characters, required):





Valid HTML 4.01 Transitional
E-Mail - 102.88 ms

Valid HTML 4.01 Transitional