DEV Community

macnux
macnux

Posted on

Bash functions

Before we talk about bash functions, let’s discuss this situation. When writing bash scripts, you’ll find yourself that you are using the same code in multiple places.

If you get tired of writing the same lines of code again and again in your bash script, it would be nice to write the block of code once and call it anywhere in your bash script.

The bash shell allows you to do just that with Functions.

Bash functions are blocks of code that you can reuse them anywhere in your code. Anytime you want to use this block of code in your script, you simply type the function name given to it.

We are going to talk about how to create your own bash functions and how to use them in shell scripts.

1 Creating a function
2 Using Functions
3 Using the return Command
4 Using Function Output
5 Passing Parameters
6 Processing Variables in Bash Functions
6.1 Global Variables
6.2 Local Variables
7 Passing Arrays As Parameters
8 Recursive Function
9 Creating Libraries
10 Use Bash Functions From Command Line

https://likegeeks.com/bash-functions/

Top comments (0)