Using Variables
Defining variables in a bash script is so simple! you don't have to worry about the datatype as you do in languages like C/C++. Just go on and define the variable like the following:
#!/bin/bash #Define a string variable, don't forget the quotes!! #if your variable is not string, omit the quotes STR="A Simple String Here" echo $STROK, don't panic about the $ sign in the echo line, here's why: When you want to use a variable's value, you have to add the $ at it's beginning. If you have worked with PHP before, you probably already know this.
Here's another example:
#!/bin/bash #define a string - it holds a directory location LOC="/home/anarion" #another for a file FIL="test.jpg" cp $LOC$FIL ./backup-test.jpgIn the above example, we combined both strings and used them both in a cp command.
Another note on the variables, you can use a program's outputs on-the-fly for another's input like this:
#!/bin/bash echo $(grep "kian" ~kian/names.txt)What this line does is: catch the output of the grep command and use it like a variable - just like our previous examples but this time we don't have a variable name... so it cannot be used in other lines of the script.
***
Now you should have learned the basic usage of variables in your scripts, in the next part we are going to learn about if statements and a little script to know if user has root privileges or not.






MultiQuote


|