Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • 3
    By far the best answer yet too many people don't use parameter expansion. This works on arrays too, incl $@: params=("${@##*[!0-9]*}") -- Keep all positional arguments, but only use numbers - anything else will be converted to null string which conveniently evaluates to 0 in arithmetic evaluations (my use case - function parameter is a list of integers, unset or null should default to 0 but caller sets variable name when unset). Commented Apr 22, 2020 at 13:13
  • getting this to work for all integers, not just non-negative, is trickier because of edge cases like scale=1-2. how to handle such a case depends on the application. Commented Mar 22, 2024 at 14:50