Variables in shell scripting
Variable is a place in memory where you can store your data like strings/texts and numbers in shell scripting.
As the name suggest variable it is not fixed it's value can be changed depending on the information passed
to the program.
Rules and conventions to create variable in shell scripting
- variable name should start with character or underscore.
This is a valid variable-
_name="my name is abc"
This is also a valid variable -
name="my name is abc"
- You cant start variable name with a numeric value at the beginning.
Name1=" My name is abc " is valid variable and
1name="My name is abc" is an invalid variable.
- Variables are case sensitive in shell scripting.
Bash interpreter will treat them differently. Bash interpreter differentiate between upper-case and lower-case.
- Comma's and blank spaces are not allowed.
Declaring variable in shell scripting
That's it. Now the variable hello is holding the string "hello viewers "
Note You can put semicolon after the variables and statements.It is optional.
Save this file and make it executable and run.
#variable declaration name="Welcome viewers" number=56 #accessing/printing variable #variables are accessed theought $ sign. echo $name echo $number;
- First statement is a comment we discussed that in our previous article.
- In our second statement we declared a variable name="Welcome viewers"; Variable name is storing value welcome viewers.
- Last 2 statements are printing name and number variables on output screen.