1

I want to get all changed files from

$git diff --name-only SHA1 SHA2

and save them to an array variable in bash, but when I use:

files=$(git diff --name-only SHA1 SHA2)

for some reason it saves all file names as a string with space delimiter, so when I type

$echo ${files[0]} 

I get all file names as a single String, and

$echo ${files[1]}

is empty. So how do I save each file name as an array element, so that I could iterate over them?

Kristina
  • 55
  • 5

1 Answers1

3

You can capture a multiline output as array in bash

files=($(git diff --name-only SHA1 SHA2))

Then echo ${files[0]} or echo ${files[1]} would work as expected.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250