Last active
February 11, 2021 01:24
-
-
Save LozanoMatheus/f92da8cc226de69bfab73972129c1d27 to your computer and use it in GitHub Desktop.
Bash variables - Explaining their differences - defining the vars outside of a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function print_vars(){ | |
echo "func_declare_g->${declare_g}<-" | |
echo "func_declare_x->${declare_x}<-" | |
echo "func_declare_only->${declare_only}<-" | |
echo "func_export_only->${export_only}<-" | |
# echo "func_local_only->${local_only}<-" | |
echo "func_env_only->${env_only}<-" | |
## the command bellow is to find all environemnts variables defined for all running process | |
env env_cmd="output_func_env_cmd" grep -l env_cmd /proc/*/environ | |
} | |
declare -g declare_g="output_declare_g" | |
declare -x declare_x="output_declare_x" | |
declare declare_only="output_declare_only" | |
export export_only="output_export_only" | |
## local vars can only be defined inside of a function | |
# local local_only="output_local_only" | |
env env_only="output_env_only" | |
print_vars | |
echo "outside_declare_g->${declare_g}<-" | |
echo "outside_declare_x->${declare_x}<-" | |
echo "outside_declare_only->${declare_only}<-" | |
echo "outside_export_only->${export_only}<-" | |
# echo "outside_local_only->${local_only}<-" | |
echo "outside_env_only->${env_only}<-" | |
## the command bellow is to find all environemnts variables defined for all running process | |
env env_cmd="output_outside_env_cmd" grep -l env_cmd /proc/*/environ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## The output of -> env env_only="output_env_only" | |
HOSTNAME=8756f7b76687 | |
TERM=xterm | |
declare_x=output_declare_x | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
PWD=/ | |
SHLVL=1 | |
HOME=/root | |
export_only=output_export_only | |
_=/usr/bin/env | |
env_only=output_env_only | |
## | |
## Outputs of the echo inside the function print_vars | |
func_declare_g->output_declare_g<- | |
func_declare_x->output_declare_x<- | |
func_declare_only->output_declare_only<- | |
func_export_only->output_export_only<- | |
func_env_only-><- | |
/proc/self/environ | |
/proc/thread-self/environ | |
## | |
## Outputs of the echo outside the function print_vars | |
outside_declare_g->output_declare_g<- | |
outside_declare_x->output_declare_x<- | |
outside_declare_only->output_declare_only<- | |
outside_export_only->output_export_only<- | |
outside_env_only-><- | |
/proc/self/environ | |
/proc/thread-self/environ | |
## | |
## Check the link below to understand the two /proc/* files | |
## See on Medium: https://medium.com/unboxing-the-cloud/bash-variables-things-that-you-probably-dont-know-about-it-8a5470887331?sk=26693ca772a0c54c99d3712303560ed4 | |
## See on my Website: https://www.lozanomatheus.com/post/bash-variables-things-that-you-probably-don-t-know-about-it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment