Pass list of arguments to a command in shell

19,335

Solution 1

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}

Solution 2

Look into the shift bash command (man bash). You can iterate, taking $1 each time.

for n in $(seq 1 $#); do
  echo $1
  shift
done

Call this file myshift.sh. Then

$ ./myshift.sh a b c 
a
b
c

Solution 3

If you're generating a list of files with seq, you can just use command substitution to drop them into the command line:

mycommand $(seq ...)

although that will fail if the filenames so generated have any spaces (or tabs or newlines...) in them.

You can also use bash's curly-brace expansion to generate them instead of seq, for instance file{1..10} to generate file1 file2 file3 file4 file5 file6 file7 file8 file9 file10. This has the advantage of working even if the filename contains spaces in the common part, as long as you quote it properly (e.g. "file name"{1..10}).

Share:
19,335
rhlee
Author by

rhlee

Updated on June 04, 2022

Comments

  • rhlee
    rhlee about 2 years

    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?

  • rhlee
    rhlee almost 10 years
    Yes, sorry about the confusion, by "list" I meant sequence of files. mycommand file{1..20} worked perfectly, thanks.
  • rhlee
    rhlee almost 10 years
    Yep, the curly bracket expansion works. It's a shame I can't mark multiple answers as the "correct one".
  • Mark Reed
    Mark Reed almost 10 years
    Well, that part of my answer was redundant with the one you accepted anyway. The only reason I answered at all was because you had added a comment about seq that hadn't been addressed yet. Not sure that's enough to leave this answer here..
  • chepner
    chepner almost 10 years
    for loops already have a standard way of iterating over command-line arguments. This is just a non-standard way of writing for n; do echo "$n"; done.
  • JayInNyc
    JayInNyc almost 10 years
    @chepner I didn't know that. Thanks!