What is the difference between local and global variable in linux bash scripts?

What is the difference between local and global variable in Linux bash scripts?
Can you please provide an example if you know them.

This is on macOS, which is Unix, but it should be the same on Linux

Local variables:

Only accessible from the terminal that defined it, not any other program.

Example:

Global variables are accessible from the terminal session that created it, as well as any program started by that terminal session.

You define global variables with ‘export’

See if you can see the difference here:

the script can see the global variable, but not the local one.

Generally, inside scripts, you want to use local variables that you define there. For global variables, you want to define them in a startup script, like .bash_profile or whatever.

1 Like

Thank you Mr. Miguel.
I got almost nothing from from that. I will see the next week in school.

Local = variable available only in the current session (terminal or script).

Global = variable available to anything started by the current session.

What is it you’re trying to do ? Might be easier to just figure that out…

1 Like