KevsRobots Learning Platform
60% Percent Complete
By Kevin McAleer, 2 Minutes
Shell scripts become more powerful when you use programming constructs like conditional statements, loops, and variables. In this lesson, you’ll learn how to use these basic scripting constructs to make your scripts more dynamic and functional.
if
, else
).for
, while
) to repeat tasks.Variables allow you to store and reuse values in your scripts. To define a variable and use it:
#!/bin/bash
name="Raspberry Pi"
echo "Hello, $name!"
Note that the variable name
should not have spaces around the =
sign. Also note when the variable is used, it is prefixed with a $
; $name
in this case.
Conditional statements enable your script to make decisions. Here’s a basic example using if and else:
#!/bin/bash
if [ -f "/etc/passwd" ]; then
echo "The file exists."
else
echo "The file does not exist."
fi
Loops allow you to repeat tasks multiple times. Here’s an example of a for loop:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Iteration $i"
done
And a while loop:
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count is $count"
count=$((count + 1))
done
In this lesson, you learned how to use variables, conditional statements, and loops in your shell scripts. These constructs make your scripts more powerful and flexible, allowing you to automate complex tasks more effectively.
You can use the arrows ← →
on your keyboard to navigate between lessons.