BASH Variables
Let's take a look at our script again:
#!/bin/bash DELAY=0.2 output_file=$1 while /bin/true do cat /proc/meminfo | \ grep 'MemFree' | \ tee -a ${output_file} sleep ${DELAY} done
The 3th line, DELAY=0.2
is a variable assignment: in bash script a variable does not have to be declared before use (as in C and other programming languages) but are created dynamically.
Variables can also be exported and become visible from all programs/scripts executed in the same window after the declaration/export, while the ones which aren't (like DELAY, assigned but not exported) are visible only in the current shell level.
To get the content of a variable, the $ operator is used: $DELAY
can be roughly seen as "value of the DELAY
variable" and will be replaced by the BASH
with 0.2, if executed in the script after the declaration of line 3 (in the script is in fact used the notation ${DELAY}
, which means the same thing and is used to disambiguate situations in which e.g. multiple variables start with the same name. man bash for details).
Keep in mind that the case of the variables is important (you should assume that case always matters in Linux anyway, unless you are sure of the contrary) and used by programmers as a de-facto rule to separate constants (traditionally upper-case) from other variables that may change overtime (lower/mixed-case).