Shell Scripting Basics Explained
Key Concepts
- Shell Script
- Shebang Line
- Variables
- Input and Output
- Conditionals
- Loops
- Functions
- Command Substitution
- Error Handling
- Comments
- Script Execution
Shell Script
A shell script is a text file containing a series of commands that the shell interpreter can execute. It allows automation of tasks and simplifies complex operations.
Example: A shell script to greet the user and display the current date.
#!/bin/bash echo "Hello, $USER!" date
Shebang Line
The shebang line, also known as a hashbang, is the first line in a shell script that specifies the interpreter to be used. It typically starts with #!
followed by the path to the interpreter.
Example: #!/bin/bash
tells the system to use the Bash shell to execute the script.
Variables
Variables in shell scripting are used to store data that can be referenced and manipulated. They are defined by assigning a value to a name.
Example: Assigning a value to a variable and displaying it.
name="Alice" echo "Hello, $name!"
Input and Output
Input and output in shell scripting involve reading data from the user or a file and displaying data to the terminal or a file. Commands like read
and echo
are commonly used.
Example: Reading user input and displaying it.
echo "Enter your name:" read name echo "Hello, $name!"
Conditionals
Conditionals allow scripts to make decisions based on certain conditions. The if
, elif
, and else
statements are used for this purpose.
Example: Checking if a number is positive, negative, or zero.
echo "Enter a number:" read num if [ $num -gt 0 ]; then echo "Positive" elif [ $num -lt 0 ]; then echo "Negative" else echo "Zero" fi
Loops
Loops are used to execute a set of commands repeatedly. The for
, while
, and until
loops are commonly used in shell scripting.
Example: Using a for
loop to print numbers from 1 to 5.
for i in {1..5}; do echo $i done
Functions
Functions in shell scripting allow you to group commands into reusable blocks. They can be called multiple times within a script.
Example: Defining and calling a function to greet the user.
greet() { echo "Hello, $1!" } greet "Alice"
Command Substitution
Command substitution allows you to use the output of a command as an argument to another command. It is done using $(command)
or backticks command
.
Example: Using command substitution to get the current date.
current_date=$(date) echo "Today's date is $current_date"
Error Handling
Error handling in shell scripting involves checking the success or failure of commands and taking appropriate actions. The exit
command and $?
variable are commonly used.
Example: Checking if a command succeeded and exiting if it failed.
ls /nonexistent_directory if [ $? -ne 0 ]; then echo "Directory not found" exit 1 fi
Comments
Comments are used to add explanatory notes to the script that are ignored by the interpreter. They start with the #
symbol.
Example: Adding a comment to explain a section of the script.
# This script greets the user echo "Hello, $USER!"
Script Execution
To execute a shell script, you need to make it executable using the chmod
command and then run it using the shell interpreter.
Example: Making a script executable and running it.
chmod +x myscript.sh ./myscript.sh