Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Linux Shell Scripting Essentials

You're reading from   Linux Shell Scripting Essentials Learn shell scripting to solve complex shell-related problems and to efficiently automate your day-to-day tasks

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781785284441
Length 282 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Sinny Kumari Sinny Kumari
Author Profile Icon Sinny Kumari
Sinny Kumari
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Linux Shell Scripting Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. The Beginning of the Scripting Journey FREE CHAPTER 2. Getting Hands-on with I/O, Redirection Pipes, and Filters 3. Effective Script Writing 4. Modularizing and Debugging 5. Customizing the Environment 6. Working with Files 7. Welcome to the Processes 8. Scheduling Tasks and Embedding Languages in Scripts Index

Using functions and positional parameters


Similar to other programming languages, function is a way to write a set of actions once and use it multiple times. It makes the code modular and reusable.

The syntax of writing a function is as follows:

function function_name
 {
  # Common set of action to be done
 }

Here, function is a keyword to specify a function and function_name is the name of the function; we can also define a function in the following ways:

function_name()
{
  # Common set of action to be done
}

The actions written within curly braces are executed whenever a particular function is invoked.

Calling a function in bash

Consider the following shell script that defines the my_func()function:

#!/bin/bash
# Filename: function_call.sh
# Description: Shows how function is defined and called in bash

# Defining my_func function
my_func()
{
  echo "Function my_func is called"
  return 3
}

my_func # Calling my_func function
return_value=$?
echo "Return value of function = $return_value...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images