Bash Scripting - Arrays Functions
Bash Scripting - Arrays Functions
Bash Scripting - Arrays Functions
1 Arrays in bash
The bash shell has two types of arrays: one-dimensional indexed arrays and associative arrays. You can
use any variable to represent an index array, or you can explicitly declare variable to be an array using
the declare keyword. Note however whichever means an array is created there is no limit to the of the
size of the array, nor is there no need to assign value contiguously.
arrayname=(value1 value2 …) creates array using the given values, indexed sequentially
unset arrayname[index#] will destroy the value of that index, whilst unset name removes the entire
array.
Pg. 1 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
Example: Initialize at the time of declaration
declare -A colours=([apple]=red [berry]=blue [banana]=yellow [grape]=purple)
The following screen shots shows how elements of an associative array can be accessed
Individually
Print indexes
Print values
Once an associate array has been initialized then the only way to add new element is to use For
example: arrayname+=([index-key]=value)
The screen shot below will print the current array’s content, then adds the element [pear]=green before
reprint array’s content.
Pg. 2 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
3 Functions
A function is used in bash to group commands together for later execution and called utilizing a single
name for the group. The use of functions enables:
Abstraction: Focusing on individual building blocks rather than the whole structure
There are two ways declare a function (1) using the name of the function and (02) using the reserved
word function.
Method 1:
name ( ) {
commands
}
Method 2:
function name {
commands
}
Spaces between curly braces and commands are required, along with a semicolon or a newline to
represent the end of commands.
#!/bin/bash
Hello ( ) { echo 'hello, world'; }
Hello
Declaring a function doesn’t execute it, we must invoke a bash function by calling the function’s name.
Also, the function’s definition must be placed before any calls to the function.
#!/bin/bash
Hello ( ) { echo “Hello, world”; }
Hello ⇐This line executes the function
3.1 Variables
In bash all variables by default are defined as global, even if declared inside the function. If you require a
variable to be used only within a specific function you must use the local keyword. Local variables can
have the same name in different functions.
Pg. 3 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
The script localV above starts by defining two global variables var1 and var2. Then two functions, both
setting a local variable var1 and modifying the global variable var2. When you run the script, you should
see the following output:
Returning values: Function in bash has a limitation as to what can be return. When a bash function
completes, its returns 0 for success or a decimal number in range 1-255 for failure. And this returned
value is stored in the variable $?
One way of returning a value from function is to use the return keyword, however with the utilization of
return you can only return a numeric value in the range 0-255.
An alternative to the return keyword is to return the value via a stdout using echo or printf. To do this
the function will have to execute via a variable assignment.
variable=$(function_name)
Pg. 4 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
Passing Arguments: To pass parameters to a bash function you will have to place them to the right of
the function when its being called (see line 8 below). In line 4 the $1 and $2 corresponds to the position
of the parameter after function name. Therefore, $1 will store $LOGNAME and $2 will store value
entered by user.
4 Activity
1. Given the following script “R-values”, what will be returned from functions ONE, TWO, THREE, &
FOUR?
Pg. 5 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
2. Compare the ls function on the left to the one on the right. What can be noted about the keyword
command?
3. Create an array with five (5) random numbers and a function that will allow users to repeat a set of
operations on created array:
Pg. 6 of 6