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 - Linux Tutorial
From the course: Linux: Bash Shell and Scripts
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
The typeset and declare commands for variables5m 7s
-
(Locked)
Looping with for/while sequences and reading input14m 20s
-
(Locked)
Defining functions and using return and exit6m 29s
-
(Locked)
Using file descriptors, file redirection, pipes, and here documents16m 52s
-
(Locked)
Control-flow case statements and if-then-else with the test command10m 11s
-
(Locked)
Using arithmetic operators2m 27s
-
(Locked)
Challenges: Using local variables in functions, loops, and arithmetic2m 44s
-
(Locked)
Solutions: Using local variables in functions, loops, and arithmetic4m 2s
-
(Locked)
-
-
-