In short, you create a files
array and expands it.
You read it like this:
VAR=...
means you are assigning a variable. VAR=(...)
means you are assigning a bash array to a variable.
$(...)
means you are running a command and capturing its output, in your case the list of files that match the given pattern that are so old and then assigning each file to an element in a bash array.
${...}
signifies that you are reading the value of a variable, for example:
$ SOMEVAR="a value"
$ echo "${SOMEVAR}"
a value
In bash $VAR
and ${VAR}
are equivalent, but the ${VAR}
notation allows you to do some extra things. Most notably echo "${VAR}iable"
will expand the $VAR
variable and concat it with the iable
string whereas echo $VARiable
will try to expand the $VARiable
variable. You can also do some advanced substitution using the ${...}
notation.
In our case we are reading the file variable with ${file...}
. Next, the [...]
indicates that we are reading a bash array. So we are trying to access an element of the files array, the bit inside the [...]
tells us which elements, this can be 1
to access the first element, 2
to access the second and so on (bash arrays are indexed from 1 not 0). @
means all elements, so we are trying to access all elements of the bash array called file
.
You can try it out with the following:
% ARRAY=("a value" "another value" "fred")
% for x in "${ARRAY[@]}"; do echo "$x"; done
a value
another value
fred
The advantage to using arrays over just space separated strings is you can include spaces in the values, like above. So in your case each file in the files
array will be treated as a separate argument and spaces in the file names will be handled correctly.
So in essence your snipit finds and collects a list of files and passes them as individual arguments to the tar command.
bash
,ksh
,zsh
,mksh
– cuonglm Aug 30 '16 at 19:19