Skip to content
Fix Code Error

Loop through an array of strings in Bash?

March 13, 2021 by Code Error
Posted By: Anonymous

I want to write a script that loops through 15 strings (array possibly?) Is that possible?

Something like:

for databaseName in listOfNames
then
  # Do something
end

Solution

You can use it like this:

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

Also works for multi-line array declaration

declare -a arr=("element1" 
                "element2" "element3"
                "element4"
                )
Answered By: Anonymous

Related Articles

  • Concatenate multiple node values in xpath
  • Data binding between two Polymer elements using polymer 1.0
  • How does PHP 'foreach' actually work?
  • Best way to replace multiple characters in a string?
  • two way data binding between nested template content…
  • Polymer and advanced css selectors using…
  • nesting polymer components doesn't work
  • How to align two elements on the same line without…
  • Head pointer not accessible when creating a method…
  • XML Schema Validation : Cannot find the declaration…
  • Creating an dynamic array, but getting segmentation…
  • What are the undocumented features and limitations…
  • Vuetify checkboxes array checks all boxes when list changes
  • Reference a slot inside the ViewModel of an Aurelia…
  • Pandas how to split vlaues of every column based on colon
  • Why does this core dumped error happen in my class?…
  • First echo missing when using bash -c over SSH
  • how to add new to onclick with javascript
  • How to run a shell script on a Unix console or Mac terminal?
  • I have tried to implement mergesort and am unable to…
  • Bash: Echoing a echo command with a variable in bash
  • Trying to embed newline in a variable in bash
  • Using StringWriter for XML Serialization
  • How to define hash tables in Bash?
  • Check number of arguments passed to a Bash script
  • Java Process with Input/Output Stream
  • How to echo with different colors in the Windows…
  • Change the location of the ~ directory in a Windows…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Check if an element is present in a Bash array
  • Can't get the desired output of this binary search…
  • How to debug a bash script?
  • eval command in Bash and its typical uses
  • Simple logical operators in Bash
  • How to add extra key-value to QuerySet lists?
  • How do I use shell variables in an awk script?
  • load and execute order of scripts
  • How can I run this simple for loop in parallel bash?
  • A variable modified inside a while loop is not remembered
  • Extract columns from data frames in a list in a…
  • Removing double quotes from variables in batch file…
  • How to split elements of a list?
  • How to specify height of nested web component custom…
  • How to use JNDI DataSource provided by Tomcat in Spring?
  • Confusing error in R: Error in scan(file, what,…
  • Object is not updated (by Lambdas filter) when…
  • Getting the closest string match
  • SQL Server: Query fast, but slow from procedure
  • What are forward declarations in C++?
  • Centering paper-item text
  • What does "-ne" mean in bash?
  • How to check in Javascript if one element is…
  • average of 2 neighbouring elements in array python
  • How do I append one string to another in Python?
  • Index X out of bounds for length Y
  • java.sql.SQLException: No suitable driver found for…
  • What does a "Cannot find symbol" or "Cannot resolve…
  • What is your most productive shortcut with Vim?
  • How to tell if a string is not defined in a Bash…
  • How to use html template with vue.js
  • Converting Oracle SQL Procedure into MySQL Stored Procedure
  • VUE Error when run test unit
  • Simplest way to create Unix-like continuous pipeline…
  • Echo a blank (empty) line to the console from a…
  • How do I parse command line arguments in Bash?
  • jQuery AJAX cross domain
  • Configure systemd to manage a service
  • How to properly do JSON API GET requests and assign…
  • How to find BMI Index Values and Weight status in C#?
  • Why is Google Firebase not recommended by Google -…
  • Iterate over the lines of a string
  • Move an array element from one array position to another
  • How does Ruby Enumerators chaining work exactly?
  • Redirect stderr to stdout in C shell
  • For-each over an array in JavaScript
  • PHP for loop Show the entries in ascending order by quantity
  • Vuetify v-tabs v-tab-item overflows window width
  • shell script. how to extract string using regular…
  • Design DFA accepting binary strings divisible by a…
  • How to remove a newline from a string in Bash
  • What makes the different performances between…
  • Exporting awk variables to bash variables?
  • Cursor inside LOOP is jumping out too early
  • Fastest way to iterate over all the chars in a String
  • Multi-dimensional arrays in Bash
  • No 'Access-Control-Allow-Origin' header is present…
  • What's the difference between [ and [[ in Bash?
  • Can Python test the membership of multiple values in a list?
  • how to dynamically append an element to dom-if in Polymer?
  • Bash - Increment a variable until it can be found in…
  • Simple bubble sort c#
  • Why does this selection sort code shows different…
  • How to use Servlets and Ajax?
  • How can I get the source directory of a Bash script…
  • Multiple separate IF conditions in SQL Server
  • Convert Set to List without creating new List
  • Free the memory after it is allocated with calloc()…
  • How to add a new row to an empty numpy array
  • Aurelia UX showcase app fails to load
  • bash export command

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to reset AUTO_INCREMENT in MySQL?

Next Post:

How do you set a default value for a MySQL Datetime column?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error