Yes you can. A possible caveat is SC2155 in that you should declare and assign separately.
That being said it will work except for using multiple declare options between the parameters. Also note that the declare parameters will apply to all variables (in this case -i
).
script0:
#!/bin/bash
declare a b c
a=foo
b=bar
c=baz
foo () {
local a=1 b=2 c=3
echo "From within func:"
declare -p a
declare -p b
declare -p c
}
foo
echo "From outside func:"
declare -p a
declare -p b
declare -p c
Output:
$ ./script.sh
From within func:
declare -- a="1"
declare -- b="2"
declare -- c="3"
From outside func:
declare -- a="foo"
declare -- b="bar"
declare -- c="baz"
script1:
#!/usr/bin/env bash
declare -i a -a b c
a=foo
b=(bar)
c=baz
foo () {
local -i a=1 -a b=(2) c=3
echo "From within func:"
declare -p a
declare -p b
declare -p c
}
foo
echo "From outside func:"
declare -p a
declare -p b
declare -p c
Output:
$ ./script.sh
./script.sh: line 3: declare: `-a': not a valid identifier
./script.sh: line 9: local: `-a': not a valid identifier
From within func:
declare -i a="1"
declare -ai b=([0]="2")
declare -i c="3"
From outside func:
declare -i a="0"
declare -ai b=([0]="0")
declare -i c="0"