2

I have three commands I running in shell file (app.sh):

docker-compose -f .docker/docker-compose.my-app1.yml push my-app1
docker-compose -f .docker/docker-compose.my-app2.yml push my-app2
docker-compose -f .docker/docker-compose.my-app3.yml push my-app3

how to create array and pass the values to command with foreach?

say if it's was nodejs I'll do:

['my-app1', 'my-app2', 'my-app3'].forEach(i => {

  … `-f .docker/docker-compose.${i}.yml push ${i}` ...;
})
1
  • If it's Linux then something like this in script. for n in $(cat lists.txt ) do echo "Working on $n file name now" # do something on $n below, say count line numbers # wc -l "$n" done Commented Feb 17, 2020 at 16:24

1 Answer 1

3

In BASH you can have arrays and for loops as follows

arr=( 'my-app1' 'my-app2' 'my-app3' )

for i in "${arr[@]}" ; do
   docker-compose -f ".docker/docker-compose.${i}.yml" push "${i}"
done

Note, that you do not need to use commas to separate array elements as whitespace is a token separator in BASH.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.