Categories
Tags
a Accessibility Advanced Algorithms Alias Bash bash Basics Beginners Best Practices Big O bun cat cd CLI Comments Container Elements Container Queries cp css CSS Data Structures deno 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 Package Manager picture pm2 Productivity programming Programming pwd Responsive Design Responsive Images rmdir Scalability section Semantic HTML shell Shell Shell Script Shells srcset State Management Structure Svelte Svelte Store SvelteKit svg Tables tail Text Formatting Tools touch Troubleshooting Tutorial Unix ux 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/