Global vs Local Variables - Bash Scripting

preview_player
Показать описание


This video has been made with Fair Use in mind and has been created as an educational piece of media.

#bash #sonarsystems #snrsys

If you like this stuff, as always, show the love through comments, likes, favorites, subscriptions, etc.

Tens of thousands of free videos at

If you have any questions feel free to post them at

Our Website

Our games made with love
------------------------------------------------------

Check out our books:
Рекомендации по теме
Комментарии
Автор

This video is totally wrong. num2 is returning null because it is being echoed BEFORE the function is being called that defines it. The true way to define a local variable inside a function is with local:

tl;dr FTFY

num1=10

Hello(){
local num2=9
echo "Inside of function num1: ${num1}"
echo "Inside of function num2: ${num2}"
}

#This isn't a local variable and will work inside and outside function
echo "Outside of function num1: ${num1}"

#This is null because it's called before the variable "num2" is defined
echo "Outside of function num2: ${num2}"

#Now call the function and see both variables expressed
Hello

#This will still return a value since it isn't local to the function
echo "Outside of function num1: ${num1}"

#This will return null since it is a local variable and only set inside the function
echo "Outside of function num2: ${num2}"

parkerhemphill