Bash Special Variables¶
Variable |
Description |
|---|---|
|
The exit status of the last executed command. |
|
The process ID (PID) of the current shell. |
|
The process ID (PID) of the last background command executed. |
|
The number of positional parameters (arguments) passed to the script or function. |
|
Expands to all positional parameters passed to the script or function as separate entities. |
|
Expands to all positional parameters as a single string, separated by the first character of the |
|
The name of the shell script or function. |
|
Positional parameters representing the arguments passed to the script or function, where |
Example Usage¶
Here is an example demonstrating the usage of these variables:
echo "Script Name: $0"
echo "Number of Arguments: $#"
echo "All Arguments (individually): $@"
echo "All Arguments (as a single string): $*"
echo "First Argument: $1"
echo "Exit Status of Last Command: $?"
echo "Process ID of Current Shell: $$"
# Simulate a background process
sleep 5 &
echo "Process ID of Last Background Command: $!"
Execute Parsed bash command¶
To execute a bash command after some shell parsing
Say we have want to manually run a command from our crontab
crontab -l | tail -n 1
To execute that in bash we would do as follows
bash -c "$(crontab -l | tail -n 1)"
Awk Guide for Substrings¶
substr(string, start, length)
string: The input string.
start: The starting position (1-based index).
length: (Optional) The number of characters to extract. If omitted, extracts to the end of the string.
Examples¶
Command |
Output |
Explanation |
|---|---|---|
|
|
First 5 characters |
|
|
Use space as delim, Next 2 chars from second field |
|
|
Char 8 to the end of the line. |
|
|
Use comma as delim, 3 chars from second field (Corresponds to 3) |
|
|
Combine substrings formed by awk |
Notes¶
The default field separator is a space. Use the
-Foption to specify a custom delimiter.Conditions can be added to extract substrings only when specific criteria are met, e.g.,
if ($2 > 100).