What Does Do In Bash

Shell Programming

In Bash, the do command is an essential part of creating loops. It is typically used in combination with the for or while commands to execute a block of code multiple times. I’m excited to dive deeper into this topic and explore the versatility of the do command.

Understanding the do Command

The do command is used to mark the beginning of the block of code that needs to be executed repeatedly. When used in conjunction with the for command, for example, do helps in specifying the set of values to iterate over. Similarly, with the while command, do helps in executing a block of code as long as a specific condition is met.

Example of Using do with for

Let’s consider a scenario where I want to iterate through a list of names and perform an action for each name. In this case, I can use the for loop in combination with do:

for name in Alice Bob Charlie; do
echo "Hello, $name"

In this example, the do command marks the beginning of the block of code to be executed for each name in the list.

Example of Using do with while

Now, let’s explore the usage of do with the while command. Suppose I want to keep prompting the user for input until a specific condition is satisfied:

while [ $input != "quit" ]; do
read -p "Enter your input: " input
# Additional logic can be added here

In this case, the block of code following the do command will be executed repeatedly as long as the condition $input != "quit" holds true.

Personal Experience with do

As someone who has been working with Bash for several years, I can say that the do command has been a lifesaver in various scripting scenarios. Whether it’s processing a large number of files, handling user input, or performing repetitive tasks, the do command, in combination with for and while loops, has always provided a reliable and efficient solution.


After delving into the intricacies of the do command in Bash, I am reminded of its fundamental role in enabling the execution of repetitive code blocks. The versatility and usefulness of do with for and while loops make it an indispensable tool in shell scripting. I encourage fellow Bash enthusiasts to continue exploring and leveraging the power of the do command in their scripting endeavors.