Skip to content

Instantly share code, notes, and snippets.

@LozanoMatheus
Last active February 11, 2021 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LozanoMatheus/a3b5ea146ed16d10bceb7f83312ddf79 to your computer and use it in GitHub Desktop.
Save LozanoMatheus/a3b5ea146ed16d10bceb7f83312ddf79 to your computer and use it in GitHub Desktop.
Bash variables - Explaining their differences - defining the vars inside of a function
#!/usr/bin/env bash
function test_vars(){
declare -g declare_g="declare_g_output"
declare -x declare_x="declare_x_output"
declare declare_only="declare_only_output"
export export_only="export_only_output"
local local_only="local_only_output"
env env_only="env_only_output"
### testing the variables defined in this function
echo "declare_g->${declare_g}<-"
echo "declare_x->${declare_x}<-"
echo "declare_only->${declare_only}<-"
echo "export_only->${export_only}<-"
echo "local_only->${local_only}<-"
echo "env_only->${env_only}<-"
## the command bellow is to find all environemnts variables defined for all running process
env env_cmd="output_env_cmd" grep -l env_cmd /proc/*/environ
}
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
}
test_vars
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
## The output of -> env env_only="output_env_only"
declare_x=declare_x_output
HOSTNAME=a179f90550e7
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
export_only=export_only_output
_=/usr/bin/env
env_only=env_only_output
##
## Outputs of the echo inside the function test_vars
declare_g->declare_g_output<-
declare_x->declare_x_output<-
declare_only->declare_only_output<-
export_only->export_only_output<-
local_only->local_only_output<-
env_only-><-
/proc/self/environ
/proc/thread-self/environ
##
## Outputs of the echo inside the function print_vars
func_declare_g->declare_g_output<-
func_declare_x-><-
func_declare_only-><-
func_export_only->export_only_output<-
func_local_only-><-
func_env_only-><-
/proc/self/environ
/proc/thread-self/environ
## Outputs of the echo outside the functions test_vars and print_vars
outside_declare_g->declare_g_output<-
outside_declare_x-><-
outside_declare_only-><-
outside_export_only->export_only_output<-
outside_local_only-><-
outside_env_only-><-
/proc/self/environ
/proc/thread-self/environ
##
### Check the link below to understand the two /proc/* files and the difference between export and declare -g
### 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