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
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
You can use the syntax of
${arrayName[@]}
or just
In the same spirit as 4ndrew's answer:
B. No whitespace in the names:
Notes
listOfNames="RA RB R C RD"
has the same output.Other ways to bring in data include:
Read from stdin
IFS='\n'
, or for MacOSIFS='\r'
)#!/bin/bash
at the top of the script file indicates the execution environment.Other Sources (while read loop)
None of those answers include a counter...
Output:
Simple way :
Yes
Output :
Over multiple lines
Output :
Simple list variable
or
Display the list variable :
Out put :
Loop through the list:
Out put :
Create a function to go through a list:
To preserve spaces ; single or double quote list entries and double quote list expansions :
Output :
Using the declare keyword (command) to create the list , which is technically called an array :
Out put :
Creating an associative array . A dictionary :
Output :
CVS variables or files in to a list .
Changing the internal field separator from a space , to what ever you want .
In the example below it is changed to a comma
Output :
If need to number them :
this is called a back tick . Put the command inside back ticks .
It is next to the number one on your keyboard and or above the tab key . On a standard american english language keyboard .
Out put is :
Becoming more familiar with bashes behavior :
Create a list in a file
Read the list file in to a list and display
BASH commandline reference manual : Special meaning of certain characters or words to the shell.