Executable Files
Store commands in a file. Make it executable:
chmod +x filename
Comments
Use # to mark comments. Everything after the # to the end of the line is ignored. # must be the first character of the line or follow whitespace.
# This comment is the whole line ls -l /etc # this comment follows a command who# this is not a comment because there is no space before it
Variables
Assign values to variables by using an equal sign (=). Do not put spaces around the equal sign. Variable names must start with a letter or underscore (a-zA-z_). Digits are reserved as we will see latter. All variables are strings, there is no data type.
count=7 # Set count to the value "7" total = 9 # This one is wrong! Do not put spaces around the equal sign!
To use a value stored in a variable, use the name with a dollar sign ($) before it. If you try to show a variable that has not been declared it will not produce an error, the value will be null (nothing). Braces ({}) can be used if the variable name is ambiguous.
echo You are number $count.
echo You came in ${count}th place.
Integer Expressions
The shell has built in integer expressions that are enclosed in $(( ... )). These are treated like variables but what ever is inside the expression is processed as integer math. Any non-numeric values are varable (the dollar sign is optional). Spaces are optional. Assignments can also be done inside expressions if they are used in other commands.
$ a=$((2+2)) $ b=$(( b - 1 )) $ echo $((c = b + a)) 7 $ echo $c 7