Skip to content

Instantly share code, notes, and snippets.

@CristianCantoro
Last active May 31, 2018 13:08
Show Gist options
  • Save CristianCantoro/f942e4dceb0bf6f8728d0f56f48c695d to your computer and use it in GitHub Desktop.
Save CristianCantoro/f942e4dceb0bf6f8728d0f56f48c695d to your computer and use it in GitHub Desktop.
Variable scoping in bash
#!/bin/bash
function hello() {
local fname="$1"
echo "Ciao, $fname"
}
echo "Ereditato dal terminale"
echo "\$USER: $USER"
echo
aname='pippo'
echo "Globale"
echo "\$name: $aname"
echo
echo "Globale (2)"
hello "$aname"
echo
echo "\$fname è locale"
echo "\$fname: $fname"
echo
# Shellcheck qui notifica gli errori:
# * SC2030
# * SC2031
# "var was modified in a subshell. That change might be lost."
# https://github.com/koalaman/shellcheck/wiki/SC2031
echo "Subshell"
(
avar='foo'
echo "Nella subshell, \$avar: $avar"
)
echo "Fuori dalla subshell, \$avar: $avar"
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment