Using $@
you can do things to a list of files in bash. Example:
script.sh:
#!/bin/bash
list=$@
for file in $list; do _commands_; done
Then i can call this program with
~/path/to/./script dir1/{subdir1/*.dat,subdir2/*}
This argument will expand to a number of arguments that becomes $list
. But now i want other arguments, say $1, $2, and this listing to be $3. So i want the expansion of dir1/{subdir1/*.dat,subdir2/*}
to occur inside the script, instead of becoming many arguments. On the command line you can do this:
find dir1/{subdir1/*.dat,subdir2/*}
And get the desired output, i.e. a list if files. So I tried things like this:
arg1=$1
arg2=$2
list=$(find $3)
for file in $list; do _commands_; done
...
calling:
~/path/to/./script arg_1 arg_2 'dir1/{subdir1/*.dat,subdir2/*}'
But without success. Some help on how to make this list expand into a variable inside the script would be well appreciated!:)
EDIT: So answers below gave the solution using these commands:
arg1="$1"
arg2="$2"
shift 2
for f in "$@"; do echo "processing $f"; done;
But out of curiosity, is it still possible to pass the string dir1/{subdir1/*.dat,subdir2/*}
to a find
command (or whichever means to the same end) inside the script, without using $@
, and obtain the list that way? This could be useful e.g. if it is preferable to have the listing as not the first or last argument, or maybe in some other cases, even if it requires escaping characters or quoting the argument.