I thought of learning bash scripting. I do not know how tough is it gonna be. I have not looked at the whole complicated script at all.
I am gonna begin with a simple bash script.
First I need to locate where my bash interpreter is located.
In my case its located in /usr/local/bin/bash
(manoj@linuxweblog.com)
(~) - which bash
/usr/local/bin/bash
Now I am making a simple bash script that would echo(print a string on my scren)
(manoj@linuxweblog.com)touch first_bash.sh
(manoj@linuxweblog.com)vi first_bash.sh
Every bash script begins with a #! ( Sha bang ) followed by the location of the bash interpreter. **#! does not determine/mean a comment**
a simple script.
first_bash.sh
-----------------------------
#Declaration of the location of bash interpreter
#!/usr/local/bin/bash
#Declaration of a String Variable
STRING="My first Batch Script"
#Printing the string variable on the screen
echo $STRING
-----------------------------
Running the bash script.
Before running bash script we need to make sure its file permissions are ok and they are executable.
Hence
(manoj@linuxweblog.com)chmod +x first_bash.sh
(manoj@linuxweblog.com)./fir st_bash.sh
~My first Batch Script
(manoj@linuxweblog.com)
- manoj's blog
- Login or register to post comments
Comments
Global and Local Variables
#!/usr/local/bin/bash
#Declare global varibale *can be used anywhere in the script*
VAR="This is global variable"
function bash {
#Declare local variable which is local to function bash only
local VAR="This is local variable"
#This will still print Global variable
echo $VAR
}
#Using function bash ,we can print local variable
echo $VAR
bash
#Print A global Variable Again
Echo $VAR