bash : arguments : add info about converting args & files to array.

This commit is contained in:
Dmitry Voronin 2023-10-30 01:50:03 +03:00
parent 447088b61e
commit ad35d05bef

View file

@ -1,7 +1,27 @@
# arguments.
## break flags.
if you insert \-\- then all input after that will be treated as non-flags.
# Arguments.
## Break flags.
If you insert \-\- then all input after that will be treated as non-flags.
```bash
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.
```bash
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
```