1. User-Defined Variables and Shell-Defined Variables
a) User-Defined Variables
variable_name="value" user_name="Alice" user_age=30 |
b) Shell-Defined Variables
HOME: The home directory of the current user.
SHELL: The path of the current shell.
PATH: The search path for commands.
USER: The username of the current user.
Examples:
home_directory=$HOME current_shell=$SHELL |
2. Parameter Expansion Tricks
Default Value:
Bash Shell Scripting Techniques
${variable:-default} echo ${my_var:-"default_value"} |
Assign Default Value
${variable:=default} echo ${my_var:="default_value"} |
Length of a Variable
${#variable} |
Substring Extraction
${variable:offset:length} echo ${string:0:5} |
3. Command Substitution
Basic Syntax
$(command)Example: current_date=$(date) |
Nested Command Substitution
Basic Syntax:
Example: files_with_extension=$(echo $(ls *.sh) | wc -w) |
4. Arithmetic Expansion
Basic Syntax
$((expression)) Example sum=$((num1 + num2)) |
5. Decimal Expansion
Using bc for Floating-Point Arithmetic:
Syntax
echo "expression" | bc Example sum=$(echo "$num1 + $num2" | bc) |
6. Tilde Expansion
Home Directory
Syntax: ~ Example: home_dir=~ |
Specific User's Home Directory
Syntax: ~username Example: john_home=~john |
7. Brace Expansion
Basic Brace Expansion
Syntax: {a,b,c} Example: echo {a,b,c} Comma-Separated List with Prefix and Suffix |
Syntax: pre{one,two,three}post Example: echo pre{one,two,three}post Number Sequence Syntax: {start..end} Examples: echo {1..5} echo {a..e} echo {01..10} |
Combining Sequence and List
Syntax: {1..3}{a,b} Example: echo {1..3}{a,b |
Nested Expansion
Syntax: {{a,b},{1,2}} Example: echo {{a,b},{1,2}} |
Hands-On:
Entire Script with all Variable Expansion used and output printed as #
Comprehensive_bash_example.sh: Bash Shell Scripting Techniques
#!/bin/bash
# 1. User-defined Variables and Shell-defined Variables user_name="Alice" user_age=30 current_shell=$SHELL home_directory=$HOME
echo "User-defined variables:" echo "User Name: $user_name" # Output: User Name: Alice echo "User Age: $user_age" # Output: User Age: 30 echo echo "Shell-defined variables:" echo "Current Shell: $current_shell" # Output: Current Shell: /bin/bash (or your current shell) echo "Home Directory: $home_directory" # Output: Home Directory: /home/username (or your home directory) echo
# 2. Parameter Expansion Tricks unset my_var echo "Parameter expansion (default value): ${my_var:-default_value}" # Output: Parameter expansion (default value): default_value echo "Parameter expansion (assign default value): ${my_var:=default_value}" # Output: Parameter expansion (assign default value): default_value echo "my_var after assignment: $my_var" # Output: my_var after assignment: default_value string="Hello, World!" echo "Length of the string variable: ${#string}" # Output: Length of the string variable: 13 echo "Substring extraction (first 5 chars): ${string:0:5}" # Output: Substring extraction (first 5 chars): Hello echo
# 3. Command Substitution current_date=$(date) echo "Command substitution (regular): Current date and time is $current_date" # Output: Command substitution (regular): Current date and time is [Current Date and Time] files_with_extension=$(echo $(ls *.sh) | wc -w) echo "Command substitution (nested): Number of .sh files in current directory: $files_with_extension" # Output: Command substitution (nested): Number of .sh files in current directory: [Number of .sh files] echo
# 4. Arithmetic Expansion num1=15 num2=4 sum=$((num1 + num2)) difference=$((num1 - num2)) product=$((num1 * num2)) quotient=$((num1 / num2)) remainder=$((num1 % num2)) power=$((num1 ** num2))
echo "Arithmetic Expansion:" echo "Sum: $sum" # Output: Sum: 19 echo "Difference: $difference" # Output: Difference: 11 echo "Product: $product" # Output: Product: 60 echo "Quotient: $quotient" # Output: Quotient: 3 echo "Remainder: $remainder" # Output: Remainder: 3 echo "Power: $power" # Output: Power: 50625 echo
# 5. Decimal Expansion num1=5.7 num2=3.2 sum=$(echo "$num1 + $num2" | bc) difference=$(echo "$num1 - $num2" | bc) product=$(echo "$num1 * $num2" | bc) quotient=$(echo "scale=2; $num1 / $num2" | bc)
echo "Decimal Expansion (using bc):" echo "Sum: $sum" # Output: Sum: 8.9 echo "Difference: $difference" # Output: Difference: 2.5 echo "Product: $product" # Output: Product: 18.24 echo "Quotient: $quotient" # Output: Quotient: 1.78 echo
# 6. Tilde Expansion home_dir=~ echo "Tilde Expansion:" echo "Home Directory: $home_dir" # Output: Home Directory: /home/username (or your home directory) echo
# 7. Brace Expansion echo "Brace Expansion:" echo "Basic: {a,b,c} ->" {a,b,c} # Output: Basic: {a,b,c} -> a b c echo "Comma-separated list: pre{one,two,three}post ->" pre{one,two,three}post # Output: Comma-separated list: pre{one,two,three}post -> preonepost pretwopost prethreepost echo "Number sequence: {1..5} ->" {1..5} # Output: Number sequence: {1..5} -> 1 2 3 4 5 echo "Letter sequence: {a..e} ->" {a..e} # Output: Letter sequence: {a..e} -> a b c d e echo "Combined sequences and lists: {1..3}{a,b} ->" {1..3}{a,b} # Output: Combined sequences and lists: {1..3}{a,b} -> 1a 1b 2a 2b 3a 3b echo "Nested expansions: {{a,b},{1,2}} ->" {{a,b},{1,2}} # Output: Nested expansions: {{a,b},{1,2}} -> a b 1 2 echo "Zero-padded sequence: {01..10} ->" {01..10} # Output: Zero-padded sequence: {01..10} -> 01 02 03 04 05 06 07 08 09 10 echo
# Practical example using all features together mkdir -p ~/brace_example/{dir1,dir2,dir3} touch ~/brace_example/{dir1,dir2,dir3}/file{1..3}.txt
echo "Created directories and files using brace expansion:" ls -R ~/brace_example # Output: lists all directories and files created in ~/brace_example
rm -r ~/brace_example
echo "Cleaned up created directories and files." # Output: Cleaned up created directories and files. |
In this guide, we explored various aspects of shell scripting and command-line operations, focusing on(Bash Shell Scripting Techniques):
User-Defined and Shell-Defined Variables: Understanding how to create and use variables in Bash, as well as leveraging built-in shell variables for efficient scripting.
Parameter Expansion Tricks: Techniques to handle default values, assign default values, extract variable lengths, and perform substring extraction.
Command Substitution: Utilizing command substitution to store the output of commands and nesting command substitutions for complex operations.
Arithmetic Expansion: Performing arithmetic operations within the shell using basic and advanced techniques.
Decimal Expansion: Conducting floating-point arithmetic using bc for precise calculations.
Tilde Expansion: Simplifying directory references with tilde expansion for both current and specific users.
Brace Expansion: Generating sequences, combining lists, and creating nested expansions to streamline command operations.
Commentaires