Skip to content

Instantly share code, notes, and snippets.

@sevenissimo
Created June 30, 2022 19:17
Show Gist options
  • Select an option

  • Save sevenissimo/26b023c26c3574961cd09138d1eca043 to your computer and use it in GitHub Desktop.

Select an option

Save sevenissimo/26b023c26c3574961cd09138d1eca043 to your computer and use it in GitHub Desktop.
Positional arguments handling on Bash scripts
#!/usr/bin/env bash
__iterate_on_args() {
for a; do echo "arg: $a"; done
}
__iterate_on_glob() {
#shopt -o nullglob
for f in *."${1:-sh}"; do
echo "file: $f"
done
}
__iterate_on_files() {
: "${1:?Missing operand}"
for f; do if [[ -f "$f" ]]; then
echo "file: '$f'"
else echo "$0: '$f' is not a valid file" >&2
fi done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment