foreach
command will execute another command against every item in an incoming list. The list can be passed directly or piped into this command. The default delimiter is a new line so this works great piping the output of file listings directly in, which have a file name per line.install
command is run three times, once for each package. A contrived, but effective example.${item}
. Remember to escape the expansion in
your command so it's resolution is deferred until the forEach
runs it internally.
Here we echo each file name followed by the contents of the file.forEach
can also iterate over JSON representations of objects or arrays. This means you can pipe in JSON from a file, a command such as package show
or any REPL operation that returns complex data. The delimiter
parameter is ignored for JSON input.${item}
. If iterating over a object, the object keys will be in ${item}
and the values will be in ${value}
.valueName
parameter to forEach
.