Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goldie-lin/e9aa4a606e9aab7e049edf32fb2ea043 to your computer and use it in GitHub Desktop.
Save goldie-lin/e9aa4a606e9aab7e049edf32fb2ea043 to your computer and use it in GitHub Desktop.
bash: local, declare, decalre -g, readonly, export in a shell funtion.
#!/usr/bin/env bash
# Author: Goldie Lin
# Description:
# * "declare" in a shell function will imply "local" property.
# * So, "declare -r" and "readonly" in a shell function are slightly different.
# * But, "declare -gr" will be the same as "readonly".
test1() {
declare a1="a1 is set."
declare -r a1r="a1r is set."
}
test2() {
readonly a2="a2 is set."
}
test3() {
local a3="a3 is set."
local -r a3r="a3r is set."
}
test4() {
declare -g a4="a4 is set."
declare -gr a4r="a4r is set."
}
test5() {
a5="a5 is set."
export a5e="a5e is set."
}
test1
echo "a1='${a1}'" # a1=''
echo "a1r='${a1r}'" # a1r=''
test2
echo "a2='${a2}'" # a2='a2 is set.'
test3
echo "a3='${a3}'" # a3=''
echo "a3r='${a3r}'" # a3r=''
test4
echo "a4='${a4}'" # a4='a4 is set.'
echo "a4r='${a4r}'" # a4r='a4r is set.'
test5
echo "a5='${a5}'" # a5='a5 is set.'
echo "a5e='${a5e}'" # a5e='a5e is set.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment