Categories
Tags
a Accessibility Advanced Algorithms Alias Authentication Bash bash Basics Beginners Best Practices Big O bun cat cd CLI Cloudflare Comments Container Elements Container Queries cp css CSS Data Structures deno DevOps Doctype Download Editors Error Examples Features figure File Watching Filesystem fish Fish Shell footer frontend Guide header Hello World History Homebrew HTML HTML5 humor img javascript JavaScript Learning less Links linux Linux ls macOS Media Queries meta Mobile-First mv Netcat Networking Node.js npm Open Source Package Manager picture pm2 Privacy Productivity programming Programming pwd Remote Access Responsive Design Responsive Images rmdir Runes Scalability section Security Self-hosting Semantic HTML shell Shell Shell Script Shells srcset State Management Structure Svelte Svelte 5 Svelte Store SvelteKit svg Tables tail Text Formatting Tools touch Troubleshooting Tunnel Tutorial TypeScript Unix ux Video Processing Vim web development Web Development web-development webdev
106 words
1 minutes
How to loop over an array in Bash
How to loop over an array in Bash
Looping over an array is a common task in shell scripting. This guide provides a method to loop over an array in a Bash script.
Shell Script
Here is a Bash script to loop over an array:
#!/bin/bash
array=("element1" "element2" "element3")
for element in "${array[@]}"; do
echo "$element"
done
Explanation
array=("element1" "element2" "element3")
: This line defines an array with three elements.for element in "${array[@]}"; do
: This loop iterates over each element in the array.echo "$element"
: This command prints the current element.
Conclusion
By using this method, you can easily loop over an array in a Bash script. This can be useful for processing lists of items.
How to loop over an array in Bash
https://zxce3.net/posts/shells/how-to-loop-over-an-array-in-bash/