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.
Defining functions and using return and exit - Linux Tutorial
From the course: Linux: Bash Shell and Scripts
Defining functions and using return and exit
- [Instructor] Let's look more carefully at Bash functions. A function, essentially is a way to give a name to a sequence of statements. So you can just use that name and it will do those statements. That's particularly handy if you want to do the same sequence multiple times, also it's good for organizing your script into pieces so it's easier to read and program it. Here we have a simple function, we're naming printhello. So the key thing here in Bash is you have the word function, then you give it a name and then on the same line you have an open curly brace. Then you type the lines that are the body of the function, then you have the close curly brace. You involve the function just like it's a command. You just say printhello because essentially you're telling Bash that you have a new command. That's what a function is like and you can type this in a shell script file or you can even type it in from the command line and you're telling the shell to memorize this function. Inside a…
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)
-
-
-