DEV Community

Cover image for Learn Bash Scripting With Me 🚀 - Day 7
Babs
Babs

Posted on • Edited on

Learn Bash Scripting With Me 🚀 - Day 7

Day 7 - Functions

In case you’d like to revisit or catch up on what we covered on Day Six, here’s the link:

https://dev.to/babsarena/learn-bash-scripting-with-me-day-6-1i1n
Enter fullscreen mode Exit fullscreen mode

A function in Bash is a named block of code that you can define once and then call (reuse) multiple times in your script or shell session.

It’s like a mini-script inside your script.

How To Declare A Function

NB- a function must be declared before it is called upon to execute. (note this cause I am about to confuse you later)

There are two ways to declare a function:

  • Style 1: with function, no parentheses needed (but you can add it if you want)
function function_name {
echo "Hello from function"
}
Enter fullscreen mode Exit fullscreen mode

🔎 Breakdown

  • function - This keyword tells Bash: "Hey, I’m about to define a function."

  • function_name - This is the name of the function. You can call it later just by typing: function_name

  • { ... } The curly braces wrap the block of commands that belong to the function. Everything inside will run when you call function_name.

Important detail:

The opening brace { must be on the same line as the function name (or else Bash might get confused).

You need a space before {.

The closing brace } should be on its own line (or at least separated properly).

  • echo "Hello from function" - This is the body of the function — the actual command(s) that will run when you call it.

  • Style 2: without function, parentheses are required

my_bash() {
echo "Hello from function"
}
Enter fullscreen mode Exit fullscreen mode

NB- parenthesis () is a must

🔎 Breakdown

  • my_bash() This is the function name followed by empty parentheses. The () tells Bash: "This is a function definition, not a normal command or variable." Unlike the function keyword style, here the parentheses are required.

  • { ... } Curly braces wrap the body of the function. Everything inside will run when the function is called.

Formatting rules:

  • There must be a space between () and {.

  • Each command inside should be on its own line (or separated by ;).

  • The closing } should be on its own line.

  • echo "Hello from function" This is the command inside the function body.

Sample Using Style 1


#!/bin/bash 

# The goal of this script is to test if the /etc/shadow file that stores password hash exists

function test_shadow {
    echo "---checking for shadow file---"
    if [ -e /etc/shadow ]; then
        echo "shadow file exists"
    fi
}
Enter fullscreen mode Exit fullscreen mode

🔎 Breakdown

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode
  • The shebang line: Tells the system to run this script using the Bash interpreter located at /bin/bash.
# The goal of this script is to test if the /etc/shadow file that stores password hash exists
Enter fullscreen mode Exit fullscreen mode


`

  • The # is used for comment or documentation


function test_shadow { ... }

  • Declares a function named test_shadow. Everything between { ... } will be executed when you call test_shadow.


echo "---checking for shadow file---"

  • Inside the function: Prints a message to show the script is about to test for the file.

    if [ -e /etc/shadow ]; then
    echo "shadow file exists"
    fi

  • if starts a conditional statement.

  • [ -e /etc/shadow ] checks if the file /etc/shadow exists.

  • -e means “file exists (any type)”.

  • ; then separates the condition from the commands that should run if it’s true.

  • echo "shadow file exists" runs only if the condition is true.

  • fi ends the if block (fi is literally if spelled backwards).

Calling the function

To execute it, you need to add this at the bottom:

test_shadow

Output Of The Script

The above image shows the output of the script

NB- Remember I mentioned "a function must be declared before it is called upon to execute. (note this cause I am about to confuse you later)"

This is actually not the case when running a function inside a function.

Running A Function Inside A Function

When you run a Bash script, the shell does two main steps:

  • Parsing (reading and analyzing)

Bash reads the whole script text first.

It breaks the text into commands, keywords, and function definitions.

At this stage, it doesn’t execute the commands — it just understands the structure.

That’s how Bash already knows that there are functions, even before they are called.

  • Execution (actually running commands)

After parsing, Bash starts executing line by line in order.

Sample Script

  • the above is a script I created, run it, share your output and see if you can explain the script or make sense of it on your own.

The output of the script

Note: If this is your first time working with Bash scripting or functions, it’s a good idea to run the scripts first and observe what they do. Afterwards, come back to read the explanations again,this will help you understand the breakdown more clearly.

Top comments (0)