Let’s continue to explore the next part of the shell script!
Function
1. Basic
- Creating a function is quite simple. We can write in two different formats:
1 2 3 4 5 6 7 8 9 10 | function_name <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator"><</span> commands <span class="token operator">></span> <span class="token punctuation">}</span> <span class="token comment">#or</span> <span class="token keyword">function</span> function_name <span class="token punctuation">{</span> <span class="token operator"><</span> commands <span class="token operator">></span> <span class="token punctuation">}</span> |
Note:
- Both work the same way, and neither is faster than either.
- In other programming languages, there are usually arguments passed to the function listed in parentheses
()
. In Bash, we will write without passing anything in parentheses. - The function definition must appear in the script before any commands can be called.
- For example:
1 2 3 4 5 6 7 8 | <span class="token shebang important">#!/bin/bash</span> example <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">#line 3</span> <span class="token keyword">echo</span> Hello World <span class="token comment">#line 4</span> <span class="token punctuation">}</span> example <span class="token comment">#line 6</span> example <span class="token comment">#line 7</span> |
- Line 3: We start defining the function by naming it.
- Line 4: In curly brackets, we can write as many commands we want.
- Lines 6, 7: Once the function has been defined, we can call it as many times as we want and it will execute those commands.
2. Pass parameters (Passing Argument)
- We usually pass parameters when we want to process some data from outside, we will pass parameters right after the function name. Eg:
1 2 3 4 5 6 7 8 | <span class="token shebang important">#!/bin/bash</span> example <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> Hello, My name is <span class="token variable">$1</span> <span class="token punctuation">}</span> example Nam example Nu |
- Results returned:
1 2 3 4 | ➜ ~ ./arguments.sh Hello, My name is Nam Hello, My name is Nu |
3. Return value
- As we know, most programming languages can return values to functions, which serves as a means for functions to send data back to where they were called. But
shell
functions do not allow us to do that. However, it allows us to return the return status. It’s like making a program or a stop command a success. Here, we usereturn
to indicate thereturn
status. For example:
1 2 3 4 5 6 7 8 9 10 | <span class="token shebang important">#!/bin/bash</span> example <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> Hello, My name is <span class="token variable">$1</span> <span class="token keyword">return</span> 3 <span class="token comment">#line 5</span> <span class="token punctuation">}</span> example Nam example Nu <span class="token keyword">echo</span> This <span class="token keyword">function</span> has a <span class="token keyword">return</span> value of <span class="token variable">$?</span> <span class="token comment">#line 10</span> |
- Line 5: Here, is the return status of a function.
- Line 10: Variable $? Contains the return status of a previously run command or function.
- Note: Usually, a return status of 0 indicates that everything was successful. A value other than 0 indicates an error has occurred.
- Result:
1 2 3 4 5 | ➜ ~ ./example.sh Hello, My name is Nam Hello, My name is Nu This <span class="token keyword">function</span> has a <span class="token keyword">return</span> value of 3 |
In addition, we will add a bit about variable scope and command override
Variable Scope
- By default, the scope of a variable is
global
. That means it can be used anywhere in the script. - If we create a
local
variable in a function, it will only work in that function. The syntax will be as follows:
1 2 | local variable_name <span class="token operator">=</span> <span class="token operator"><</span> variable_value <span class="token operator">></span> |
- For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token shebang important">#!/bin/bash</span> var_example <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> local variable1 <span class="token operator">=</span> <span class="token string">'local 1'</span> <span class="token keyword">echo</span> Inside function: variable1 is <span class="token variable">$variable1</span> <span class="token keyword">:</span> variable2 is <span class="token variable">$variable2</span> variable1 <span class="token operator">=</span> <span class="token string">'variable1 changed again'</span> variable2 <span class="token operator">=</span> <span class="token string">'variable2 changed again'</span> <span class="token punctuation">}</span> variable1 <span class="token operator">=</span> <span class="token string">'global 1'</span> variable2 <span class="token operator">=</span> <span class="token string">'global 2'</span> <span class="token keyword">echo</span> Before <span class="token keyword">function</span> call: variable1 is <span class="token variable">$variable1</span> <span class="token keyword">:</span> variable2 is <span class="token variable">$variable2</span> var_example <span class="token keyword">echo</span> After <span class="token keyword">function</span> call: variable1 is <span class="token variable">$variable1</span> <span class="token keyword">:</span> variable2 is <span class="token variable">$variable2</span> |
- Result:
1 2 3 4 5 | ➜ ~ ./var_example.sh Before <span class="token keyword">function</span> call: variable1 is global 1 <span class="token keyword">:</span> variable2 is global 2 Inside function: variable1 is local 1 <span class="token keyword">:</span> variable2 is global 2 After <span class="token keyword">function</span> call: variable1 is global 1 <span class="token keyword">:</span> variable2 is variable2 changed again |
Overriding Command
- It is possible to name a function with the same name as a command we often use. For example:
1 2 3 4 5 6 7 | <span class="token shebang important">#!/bin/bash</span> <span class="token function">ls</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">command</span> <span class="token function">ls</span> -1 <span class="token comment">#Hiện thị tên file/thư mục trong thư mục đang gọi</span> <span class="token punctuation">}</span> <span class="token function">ls</span> |
- Results returned:
1 2 3 4 5 6 7 8 | ➜ ~ ./overriding.sh build Desktop Documents Downloads examples.desktop example.sh |