Wednesday, April 26, 2023

How to use for loop in Linux? Looping through List, values, and range in Bash with Example

If you know how to use for loop in bash shell or Linux, you can speed up a lot of task, saving precious time while doing support or troubleshooting in Linux. One of my favorite use of for loop is repeating same task among multiple host. For example, If I have to clean up some files from 10 hosts in the network, instead of manually going and removing files from individual host, you can use for loop and ssh command to automatically remove old log files from all the host, without manually logging into them. This is a tremendous time saving trick if you are working for an application which is running on more than one host. It's a life-saver if your application is running on 30 to 40 hosts. 

Here is an example of how you can use for loop to remove old log files from multiple hosts in Linux without manually logging into each of them. 

$ for host in 1000 1001 1002 1003 1004 1005; 
do ssh "nys${host}" "rm -rf /app/data/logs/*22032017*.log"; 
done

This command will execute SSH command for each of these hosts and remove any log file with date 22032017. This means you can do a job in 2 minutes which usually require more than 15 minutes if you try to do it manually. 

You can use for loop to write bash scripts but you can also use it directly from command line for some repetitive task. In the next paragraph, I'll show you how to execute for loop command in Linux in one line right from the command prompt. 




How to loop through a list in Linux? Example

Let's see a couple of more examples of for loop in bash shell or Linux. The for loop is similar to any other programming language e.g. you can use it for repetition but it is not index based, instead it is list based. This is very much similar to the enhanced for loop of Java 1.5. The statement written between do and done keyword are executed once for each value. You can think it as body of for loop. 

here is an example of for loop in Linux to print each number in the list

$ for n in 1 2 3 4 5; do echo $n; done;
1
2
3
4
5

Here n is the variable, while 1 2 3 4 5 is the list. The body of the for loop i.e. the statement between do and done is just an echo statement to print each number in the list. When you use for loop from command-line make sure you type everything in one line and use ; to segregate header and body of for loop.

Though, Linux for loop is not just limited to static values used in the same line, you can also use use range values as we'll see in next example. 




How to for loop values in range in Linux

You can do by specifying range in for loop as {1..10} which means the loop will go from one to 10. this is how you can print values from one to 10 in Linux using for loop

$ for n in {1..10}; do echo $n; done;
1
2
3
4
5
6
7
8
9
10
A more practical example is again going to multiple host and doing stuff. For example, let's say you have 5 hosts whose numbers starts from 1000 to 1005, instead of writing each of them as list in for loop header, you can do following to get the same result as we got in first example in this tutorial.

for host in {1000..1005}; 
do ssh "nys${host}" "rm -rf /app/data/logs/*22032017*.log"; 
done

This is more terse and you can not only save some keystrokes but also you can scale to more values easily. 




How to step through a list by skipping

Let's say you have a list of 100 servers and you only want to login into every second server, you can do that by skipping in for loop as shown below. 

How to use for loop in Linux? Looping through List, values, and range in Bash with Example


The C or Java Style for loop in Linux

Even though common usage of for loop as shown in first example is more popular on bash scripts and among other Linux users, you can still write a C or Java style classic for loop in Linux as shown below:

$ for ((i=0; i<10; i++)); do echo $i; done;
0
1
2
3
4
5
6
7
8
9

Just remember that we have two brackets (( ... )) instead of one in C or Java. The body of for loop remains unchanged as you still need do and done instead of begin and end and the code which will need to repeated will go between them. 


That's all about basic examples of for loop in bash shell or Linux. It is one of the wonderful command which can simplify a lot of repetitive task and can save tons of time, especially if you have to repeat the same thing across multiple hosts or file. You can use for loop to login to a list of host and check their stats or you can use for loop to copy a list of files from one directory to another.

Other Linux command Tutorials for Beginners and Experienced
  • 10 examples of the xargs command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • 10 Best Linux Courses for Programmers and Developers (Courses)
  • 10 Books every Linux Power user should read (books)
  • 5 Free Courses to learn Linux for Beginners (Free courses)
  • 10 examples of lsof command in Linux (examples)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 6 Free Courses to learn Bash scripting in-depth (free courses)
  • 10 examples of curl command in Linux (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 10 examples of cut command in Linux (examples)
  • 10 examples of Vim in UNIX (examples)

Thanks for reading this article so far. If you like this Linux for loop  tutorial and examples, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
 

No comments :

Post a Comment