wiki/help/linux/bash/basic/arguments.md

704 B

Arguments.

Break flags.

If you insert -- then all input after that will be treated as non-flags.

rm -- -r -f # this will remove "-r" and "-f" files instead of recursive and force.

Loop over arguments.

Handy if you want to loop over files both from arguments or filesystem that include spaces and other special chars. Basically we convert them all to an array and work with it.

local targets=("${@}") # get array of arguments.

# if arguments are empty.
if [[ "${targets}" = "" ]]; then
  targets=(*.tar)      # select all .tar files by default.
  count=${#targets[@]} # calculate the array size.
fi

# iterate.
for target in "${targets[@]}"; do
  # something.
done