If I have a list of files say file1
... file20
, how to I run a command that has the list of files as the arguments, e.g. myccommand file1 file2 ... file20
?
If your list is in your argument vector -- that is to say, if you were started with:
./yourscript file1 file2 ...
then you'd run
mycommand "$@"
If your list is in an array:
mycommand "${my_list_of_files[@]}"
If your list is in a NUL-delimited file:
xargs -0 -- mycommand <file_with_argument_list
If your list is in a newline-delimited file (which you should never do for filenames, since filenames can legitimately contain newlines):
readarray -t filenames <file_with_argument_list
mycommand "${filenames[@]}"
If by "list", you just mean that you have a sequence of files:
mycommand file{1..20}
...or, to build an array of filenames with numeric components from a range more explicitly in cases where {1..20}
doesn't work (such as when 20
is a variable):
max=20 # to demonstrate something you can't do with brace expansion
list=( )
for ((i=0; i<max; i++)); do
list+=( file"$i" )
done
mycommand "${list[@]}"