Line At A Time
Lesson 2: Comments and Variables
Comments and Variables Explain your script to humans, and store a value in it.
1#!/bin/bash
2# greet.sh: says hello to somebody
3name="Ada"
4echo "Hello, $name!"
Terminal
$ ./greet.sh
Hello, Ada!
Every line, explained
#!/bin/bash
This first line is called the shebang (from "hash bang", the # and ! it starts with). It is not a comment: it tells the computer which program should read the rest of the file. #!/bin/bash means "run this with bash". Every bash script starts with this exact line.
# greet.sh: says hello to somebody
Anything after a # is a comment: bash ignores it completely. Comments are notes to the humans who read the script later, including you next month. A one-line note at the top saying what the script is for is the most useful comment you will ever write.
name="Ada"
Variables work exactly as they do at the prompt: name on the left, = with no spaces around it, value on the right. Scripts use them constantly, because a value written once and used many times is a value you can change in one place.
echo "Hello, $name!"
The $ means "the value in the box", so this prints Hello, Ada! Change the name on the line above and the greeting changes with it.