Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass list of arguments to a command in shell

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?

like image 530
rhlee Avatar asked Feb 13 '23 10:02

rhlee


1 Answers

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}
like image 188
Charles Duffy Avatar answered Feb 24 '23 15:02

Charles Duffy