Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash | ||
---|---|---|
Prev | Chapter 3. Tutorial / Reference | Next |
Assigning reserved words or characters to variable names.
var1=case # Causes problems. var2=23skidoo # Also problems. Variable names starting with a digit are reserved by the shell. # Try var2=_23skidoo. Starting variables with an underscore is o.k. var3=xyz((!* # Causes even worse problems. |
Using a hyphen or other reserved characters in a variable name.
var-1=23 # Use 'var_1' instead. |
Using white space inappropriately (in contrast to other programming languages bash can be finicky about white space).
var1 = 23 # 'var1=23' is correct. let c = $a - $b # 'let c=$a-$b' or 'let "c = $a - $b"' are correct. if [ $a -le 5] # 'if [ $a -le 5 ]' is correct. |
Using uninitialized variables (that is, using variables before a value is assigned to them). An uninitialized variable has a value of "null", not zero.
Mixing up = and -eq in a test. Remember, = is for comparing literal variables and -eq is for numbers.
if [ $a = 273 ] # Wrong! if [ $a -eq 273 ] # Correct. |
Commands issued from a script may fail to execute because the script owner lacks execute permission for them. If a user cannot invoke a command from the command line, then putting it into a script will likewise fail. Try changing the attributes of the command in question, perhaps setting the suid bit (as root, of course).
Using bash version 2 functionality (see below) in a script headed with #!/bin/bash may cause a bailout with error messages. Your system may still have an older version of bash as the default installation. Try changing the header of the script to #!/bin/bash2.
Making scripts "suid" is generally a bad idea, as it may compromise system security. Administrative scripts should be run by root, not regular users.