Bash scripting 101: chapter 2: hello world(first real shell script)
In the introduction we didn't really use a text editor, in this chapter i'm going to assume that you at least have...
- Tried some different text editors
- Found one you like
- Learned to use it so that you can at least:
- open a file
- enter text
- edit text
- save the file
- close the text editor.
Almost every programming language tutorial starts out with a "hello world" program, pretty much i despise these programs but we'll use it anyway, it's traditional, but we'll do it untraditionally.
#!/bin/bash
# the line above is the shebang line
function GREETING
{
echo -e "Hello World!\n"
}
GREETING
The above with print: Hello world! to the screen (with a newline after it)
It's more complex than need be, because i went and did a dirty on ya, i started using functions (subroutines). A subroutine is "a program within the program".
so i'll examine it line by line again:
- #!/bin/bash --this is the shebang line
- # the line above... --this is a comment, it's ignored by bash but can be handy to make notes in
- function GREETING --tell bash that we are going to use a function, and that we want to call it "GREETING"
- { -- start the block of commands that will make up our function (subroutine)
- echo -e "Hello world!\n" --this you know from the introduction
- } -- end the block of commands that make up our function
- GREETING -- call (run/execute) our function
see, it wasn't as bad as it looked was it?
so can we do anything else funny without getting too complex? yeah we can :-)
#!/bin/bash
# the line above is the shebang line
HELLO_WORLD="Hello World!\n"
HELLO_PERSON="Hello, how are you?\nI see your name is:"
echo -e "type your first name and hit enter please\n"
read -e YOUR_NAME
function GREETING
{
local YOUR_NAME_LOCAL=$1
echo -e $HELLO
echo -e "$HELLO_PERSON $YOUR_NAME_LOCAL, that's a nice name\n"
}
GREETING $YOUR_NAME
as you can see it's a modified version of the first "hello world" one
- #!/bin/bash
this is the shebang line
- # the line above...
this is a comment, it's ignored by bash but can be handy to make notes in
- HELLO_WORLD="Hello World!\n"
Make a variable named "HELLO_WORLD" and put into it "Hello World!\n"a variable is basically a word/name that can be used to contain information, in programmer parlance "a variable can contain a string", think of it as a box into which you can put content
- HELLO_PERSON="Hello, how are you?\nI see your name is:"
Make a variable named "HELLO_PERSON" and put the text into it, please notice the newline (\n), also note i don't use an \n at the end...we'll be adding to this line further on (point 11 )and will add the newline at the end there instead
- echo -e "type your first name and hit enter please\n"
print/echo to the screen "type your first name and"...
- read -e YOUR_NAME
read into variable YOUR_NAME using readline function (the -e) which means you can edit it whilst entering it easier
- function GREETING
tell bash that we are going to use a function, and that we want to call it "GREETING"
- {
start the block of commands that will make up our function (subroutine)
- local YOUR_NAME_LOCAL=$1
define a "local variable" one that only exists within the function with the name YOUR_NAME_LOCAL and make it contain the contents of the special variable $1 (see point 13)
- echo -e $HELLO
echo/print the variable $HELLO_WORLD with escaped characters being interpreted (notice..when you CALL/USE the variable it has a $ preceeding it, when you define it, it does NOT!)
- echo -e "$HELLO_PERSON $YOUR_NAME_LOCAL, that's a nice name\n"
echo/print the variable $HELLO_PERSON the print the variable $YOUR_NAME_LOCAL on the same line as well and then print the text ", that's a nice name" and top it off with a newline
- }
end the block of commands that makes up our function
- GREETING $YOUR_NAME
call (run/execute) our function, but pass the contents of $YOUR_NAME variable to the funtion as an argument, inside the function, it will be available (first argument after function name) as $1, second argument will become $2 and so on, NOTE!!! this also works on the main script $1 will be "booboo" if you enter "scriptname booboo"
also NOTE!!! that the $1 for the "entire script" isn't the same as the $1 for the functions, one might say that each function has it's own $1
do we really have to do such a complex hello world? wouldn't something along the lines of:
#!/bin/bash
echo "Hello world!"
have worked? it sure would have worked, i wanted to introduce some good features early on though, i'm sure that if you think about it, you learned somethings about functions, variables, passing variables to functions as arguments and setting variables from other variables, arguments, and local and global (normal) variables, printing variables (and some other stuff). Not bad for that small amount of reading huh? (good thing i didn't tell you how much i was going to introduce or ya would have screamed huh? :-)))