Etheryte
Etheryte

Reputation: 25328

Bash typeset (declare) as integer without variable

Is it possible to typeset -i (synonymous with declare -i, see a manpage or a reference) in bash without assigning to a variable?

Consider the following example:

typeset -i a=42;
foo $a;

Is it possible to achieve the same functionality without using a helper variable?
Assume foo is not editable (for example, a binary) with reasonable ease.

Upvotes: 0

Views: 539

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295650

Put the declaration of type inside the function's body. You can use either declare or (to be more explicit) local for this:

foo() {
  local -i arg=$1
  ....
}

No other solution is possible without modifying the function's body (or adding a wrapper which performs typechecking before passing the arguments as untyped strings), as arguments to functions (and to external commands) are passed as strings, regardless of any type declarations which may have been made beforehand.

Upvotes: 2

Related Questions