Bash Scripting

Contents
Introduction
Simple script
Variables
How to call a script
How to call a script from a script
How to call a script
Result of commands in variable
Command line arguments
Conditional operators if else
Cycle for
Combine the lines
Monitoring memory consumption
Working with files in the directory

Introduction

Before reading this article, make sure you are familiar with the content of the article. «Basics of Bash»

Basics

At the beginning of the script you need to specify #!/bin/bash and preferably some scripter

#!/bin/bash # Comment example

Variables

Let’s say we want to call a script from a file. script_1 and put a variable in there, and then log it.

#!/bin/bash # Read from the terminal and write to the log echo $1 << ~/log.txt

echo $1 means we take the first variable.

~/ means that the file log.txt will be in the home directory

How to call a script

First you need to make sure that the file script_1 is executable

If you are in the same diretoria as script_1 execute

ls -la

-rw-rw-r--. 1 andrei andrei 5252 Nov 26 12:27 script_1

If the fourth symbol is - The file is not executable.

You can make it executable by command.

chmod u+x script_1

ls -la

-rwxrw-r--. 1 andrei andrei 5252 Nov 26 12:29 script_1

Notice the -rwx now the fourth character is x.

In mine CentOS Executable file highlighting is configured, so it's now green.

Now you can execute the script command.

./script_1

Specifically, this script should accept command-line arguments therefore we will

./script_1 word1 word2

Word1 is added to log.txt and word2 is not, because word1 is the first argument and word2 is the second.

To write down all arguments, replace $1 with $*

Add to the log the execution time commands (details) here)

#!/bin/bash # Read from the terminal and write to the log echo $(date + "%Y-%m-%d %H:%M:%S,%3N") $* << ~/log.txt

./script_1 one two three
cat log.txt

2020-09-20 17:48:27,404 one two three

How to call a script from another script

Let's say from the script. parent.sh You need to call the script. child.sh

#!/bin/bash # parent bash ./child.sh

If they are in the same directory, if they are in different directories, write the path to the script.

How to call a script

You can check if the name is in command type.

For example, you decided to call your script test.

type test

test is a shell builtin

So the name is taken, of course you can use it, but There will be additional difficulties with the challenge.

Output commands to variable

It is often necessary to execute from a command script and assign the result to a variable.

To do this, use the syntax.

CHANGE=$(Team)

For example, you need to analyze the log for the number of errors and warnings.

#!/bin/bash ERROR=$(grep -o -i ERROR 2026-10-23-log.txt | wc -l) WARNING=$(grep -o -i WARNING 2026-10-23-log.txt | wc -l) Errors: $ERROR; Warnings: $ WARNING

Wc is a command that counts the number of words.

Command line arguments

A script can take arguments from the command line

The first argument is denoted as $1 second as $2 and so on.

Let's look at an example. ar.sh

#!/bin/bash AR=$1 echo $AR $AR $AR

This script waits for one external argument and assigns its value to the AR variable.

Then the same value is derived three times.

./ar.sh hei

hei hei hei

In C similar function int argc, char* argv[]

In Python - sys.argv

Cycle for

In the most familiar form, the for cycle can be written like this.

#!/bin/bash for i in `seq 0 9`; do echo $i done

Combine the lines

Concatenation of lines can be done as follows:

concat.sh

#!/bin/bash STR1="heihei" STR2=".ru" STR3=$STR1$STR2 echo $STR3

./concat.sh

heihei.ru

Article author: Andrei Olegovich

Related articles
Bash
Bash Scripting
-eq -gt …: Comparison operators
if else: Conditional operators
while loop
date: Date
Newline
Arithmetic operations
Pass a file or password over SFTP in a Bash script
Errors Bash

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: