Kahibaro
Discord Login Register

2.7.4 Loops

Why Loops Matter in Shell Scripts

Loops let your shell script repeat actions automatically. Instead of writing the same command many times, you write it once and tell the shell how many times, or for which items, it should run.

In shell scripting you will mainly use two kinds of loops: for loops and while loops. There is also until, which is similar to while but with opposite logic. Loop concepts combine with variables and conditionals, which are covered in their own chapters, so here we focus on how the loop structures themselves work and how they look in real scripts.

A loop repeats a block of commands. It must have:

  1. A clear set of values to iterate over, or a condition to check.
  2. A way to eventually stop, or it will run forever.

The `for` Loop

A for loop repeats commands for each item in a list. The list can be written manually, created from a command, or come from shell expansion like *.txt.

A basic for loop looks like this:

for item in one two three
do
    echo "$item"
done

The shell goes through the list one, two, three. For each value it assigns that value to the loop variable item, then runs the commands between do and done.

The loop variable name is your choice. You will often see names like file, i, user, or line. The loop variable is just a shell variable during the loop, so you reference it with $item or "${item}".

You can also loop over the result of a command substitution. For example:

for user in $(cut -d: -f1 /etc/passwd)
do
    echo "User: $user"
done

Here the command inside $(...) runs first, its output is split into words, and the for loop iterates over those words. This approach has limitations with spaces and special characters, but the main idea is that for reads a list of items and walks through them.

Another common pattern is looping over files that match a pattern. For example:

for file in *.log
do
    echo "Processing $file"
done

In this case the shell expands .log to all matching files before the loop starts. If there are no matches, behavior depends on shell options, but in a default bash setup .log may remain as the literal string if no .log files exist.

In a for loop:

  • The list after in is expanded before the loop runs.
  • The loop variable receives one item at a time.
  • The body between do and done runs once per item.

The `for` Loop Without an Explicit List

There is a second form of for that uses the script arguments or the current positional parameters. This form omits the in list:

for arg in "$@"
do
    echo "Argument: $arg"
done

The list in this case comes from "$@", which represents the script’s arguments as separate words. You will see more on positional parameters in scripting practice, but the key is that for can iterate over whatever list you feed it, including built in shell lists.

You can also omit the in part entirely. In that case, bash loops over the current positional parameters directly:

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!