From the course: Linux: Bash Shell and Scripts

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The typeset and declare commands for variables

The typeset and declare commands for variables - Linux Tutorial

From the course: Linux: Bash Shell and Scripts

Start my 1-month free trial

The typeset and declare commands for variables

- [Narrator] Let's talk about local variables and the typeset command. When we mean a local variable, we mean a variable that is private inside of a function. And when it's changed in the function, it doesn't affect a variable outside of the function. This is important because if you write a fairly complicated shell script, you may have variables you use in the script that you overlook, and you assign to a variable the same name in a function and you change the global one. That could be pretty confusing and a tricky bug. So if you have variables in a function that you only need in the function, then it's good practice to declare them to be local. And you could do that by declaring them with the typeset command. If the variable's only going to have integer values then you can say typeset -i. And that makes the arithmetic faster. In fact, a little benchmark I did, it was like 10 times faster. Also, if you declare a variable to be an integer then bash lets you use some integer operations…

Contents