Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Operating system notes

Rating
-
Sold
-
Pages
6
Uploaded on
23-06-2024
Written in
2023/2024

In this you will get notes related to bash scripting of operating system

Institution
Course

Content preview

BASH SCRIPTING


Bash Functions
Functions in bash scripting are a great option to reuse code. A Bash function can be defined as a set of
commands which can be called several times within bash script. The purpose of function in bash is to
help you make your scripts more readable and avoid writing the same code again and again. It also
allows the developers to break a complicated and lengthy code to small parts which can be called
whenever required. Functions can be called anytime and repeatedly, which will enable us to reuse,
optimize, and minimize the code.

Following are some key points about bash functions:

• A function has to be declared in the shell script before we can use it.
• Arguments can be passed to the functions and accessed inside the function as $1, $2, etc.
• Local variables can be assigned within the function, and the scope of such variables will only be
that particular function.
• Built-in commands of Bash shell can be overridden using functions.


Syntax
The syntax for declaring a bash function can be defined in two formats:

1. The first method starts with the function name, followed by parentheses. It is the most preferred and
commonly used method:

function_name () {
commands
}

Single line version can be mentioned as below:

function_name () { commands; }

2. The second method starts with the function reserved word, followed by the function name:

function function_name {
commands
}

Single line version can be mentioned as below:

, function function_name { commands; }

Compared to most of the programming languages, Bash functions are somewhat limited. Let?s
understand the concept with the help of some examples:


Example: Method 1
#!/bin/bash

JTP () {

echo 'Welcome to Javatpoint.'

}

JTP




Example: Method 2
#!/bin/bash

function JTP {

echo 'Welcome to Javatpoint.'

}

JTP




Passing Arguments
Like most of the programming languages, we can also pass the arguments and process the data in bash
functions. We can insert the data to the function in a similar way as passing-command line arguments
to a bash script.

To pass any number of arguments to the bash function, we are required to insert them just after the
function's name. We must apply spaces between function name and arguments. It will also be a great
choice to use double quotes around the arguments to prevent misparsing of the arguments with spaces
in it.

Following are some key points about passing arguments to the bash functions:

• The given arguments are accessed as $1, $2, $3 ... $n, corresponding to the position of the
arguments after the function's name.
• The $0 variable is kept reserved for the function's name.

Written for

Institution
Course

Document information

Uploaded on
June 23, 2024
Number of pages
6
Written in
2023/2024
Type
Class notes
Professor(s)
Mr ahmed hussain
Contains
All classes

Subjects

$3.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
tanmaysharma

Also available in package deal

Get to know the seller

Seller avatar
tanmaysharma Pandit Deendayal Upadhyaya shekhawati University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
2
Last sold
-
Notes for BCA

I this I will provide you notes for BCA 2nd semester exam

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions