Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.

Fish Shell is preferred to Bash/Zsh. The following content is for Bash/Zsh only.

By default, variables defined in a function are global, i.e., they are visible outside the function too.

Value of x inside the function: 2
Value of x outside the function: 2

Declaring a variable as local make it visible only in the function.

Value of x inside the function: 2
Value of x outside the function: 

You can declare a variable as local multiple times (which is different from other programming languages), but of course, only the first local declaration of a variable is necessary and the following local declaration of the same variable are useless and redundant.

Value of x inside the function: 2
Value of x outside the function: 

If you have a global variable in a function and then declare it as local. The last value before it is declared as local is still visible outside the function.

Value of x inside the function: 2
Value of x outside the function: 1

By default, loop variables are global. However, you can declare them as local before using them.

1
2
3
Value of i inside the function: 3
Value of i outside the function: 3

1
2
3
Value of i inside the function: 3
Value of i outside the function: