Module 5 (Bash Script)
Black Hat Bash book
Last updated
Black Hat Bash book
Last updated
Shell Scripting – Shell Variables
Valid Variable Names
Invalid variable names
Errors will inevitably occur when you’re developing bash scripts. Luckily, debugging the script is quite intuitive. An easy way to check for mistakes early is by running the script using the -n
parameter. This parameter will read the commands in the script but won’t execute them, so if there are any syntax errors, they will be shown on the screen. you can think of it as a dry-run method to test validity of syntax:
bash -n script.sh
ipt.sh You can also use the -x
parameter to turn on verbose mode
bash -x script.sh
If you want to start debugging at a given point in the script, you can do this by including the set command in the script itself.
#!/bin/bash
set -x
--snip--
set +x
The following rules govern the naming of bash variables:
• They can include alphanumeric characters.
• They cannot start with a numerical character.
• They can contain an underscore (_).
• They cannot contain whitespace.
Unassigning Variables
Scoping Variables
Global variables are those available to the entire program. But variables in bash can also be scoped so that they are only accessible from within a certain block of code. These variables are called local variables and are declared using the local keyword. The following script shows how local and global variables work:
remembering to set the executable permission using chmod, and run it using the following command: ./local_scope_variable.sh
chmod +x script.sh => -x to be execution
Note
Use
/dev/nul
l with caution. You may miss out on important errors if you choose to redirect output to it. When in doubt, redirect standard streams such as standard output and standard error to a dedicated log file instead.
Operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo
+=
Incrementing by a constant
-=
Decrementing by a constant
Arrays
Operator
Description
&
Sends a command to the background.
&&
Used as a logical AND. The second command in the expression will beevaluated only if the first command evaluated to true.
( and )
Used for command grouping
;
Used as a list terminator. A command following the terminator will run after the preceding command has finished, regardless of whether it evaluates to true or not.
;;
Ends a case statement.
|
Redirects the output of a command as input to another command.
||
Used as a logical OR. The second command will run if the first one evaluates to false.
Redirection Operators
Operator
Description
>
Redirects stdout to a file
>>
Redirects stdout to a file by appending it to the existing content
&> or >&
Redirects stdout and stderr to a file
&>>
Redirects stdout and stderr to a file by appending it to the existing content
<
Redirects input to a command
<<
Called a here document or heredoc, redirects multiple input lines to a command
|
Redirects output of a command as input to another command
Syntax
Example add Tow Numbers and sum:
this is function showuptime
Using local
varibale