Sitemap
DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Linux — Shell Scripting/Functions

3 min readJul 11, 2024

--

The purpose of this hands-on training is to teach you how to use functions in shell.

Learning Outcomes

At the end of this hands-on training, you will be able to;

  • explain and use functions in the shell.

Outline

  • Part 1 — Creating Functions
  • Part 2 — Passing Arguments to Functions
  • Part 3 — Returning Values from Functions
  • Part 4 — Nested Functions
  • Part 5 — Variables Scope

Part 1 — Creating Functions

  • A Bash function is a piece of code that only runs when it is called. Functions enable you to reuse code. We can call the functions numerous times.
  • Create a folder and name it functions.
mkdir functions && cd functions
  • It is pretty easy to declare and call a function. Create a script file named functions.sh.
#!/bin/bash
Welcome () {
echo "Welcome to Linux Lessons"
}
Welcome
  • Make the script executable and execute it.
chmod +x functions.sh
./functions.sh

Part 2 — Passing…

--

--

DevOps.dev
DevOps.dev

Published in DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.