Thursday, November 29, 2012

Bash Special/Reserved/Internal Variables

Bash can do a WHOLE LOT MORE than most people think. This is because there are a lot of bash features that people dont know about. Take it's special reserved variables for instance:

http://tldp.org/LDP/abs/html/internalvariables.html

You can make your bash scripts do some pretty complex stuff using some of those variables.

Reserved variables are also another reason its suggested NOT to have your variables be all in UPPERCASE, because you never know what you might accidentally use.

for instance:

cat -n file.txt | grep $LINENO #Wont work as expected
cat -n file.txt | grep $lineno #will work as expected

This is because $LINENO has a special meaning, it means the line number of current line its being called on.

so if the $LINENO variable is being called on line 15 of the script, it will ALWAYS grep the 15th line of file.txt. $lineno is lower case, and therefore a completely different variable, which is defined by you.

No comments:

Post a Comment