On 01.12.2021 14:46, Stefan W wrote:
> Hi! I'm writing very ordinary script. It processes some standard list of
> files being called with no arguments, and it handles passed files if
> there are arguments. Nothing strange.
>
> I'm know that correct iteration of list of arguments in bash could be
> simple as:
>
> for i in "$@"; do
> echo $i
> done
>
> [...]
>
> But if I put in into variable, things got worse:
>
> files="$@"
>
> for i in $files; do
> echo $i
> done
>
> [...]
>
> I know that it is possible to use `if` and iterate with `for arg in
> "$@";`. But I want to have only one universal algorithm for file
> processing, and just prepare a list of files and then pass it to function.
>
> How could I have correct list of filenames (probably containing spaces)
> in correctly iterable variable in bash?
Assigning to simple scalar variables make the program parameters flat,
that's hardly avoidable.
If you want to keep an existing array structure intact then use shell
arrays as in
files=( "$@" )
for i in "${files[@]}"; do
echo $i
done
Janis