For those who are awake!
Discount -15%
We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
New design
Control panel
  • Русский
  • Українська
  • English
  • UAH
  • USD
  • RUB
  • EUR
  • 0-800-307-307 Hotline
  • +38 (044) 392-74-33 Kiev
  • +38 (057) 728-39-00 Kharkiv
  • +38 (056) 794-38-31 Dnipro
  • +38 (032) 229-58-93 Lviv
  • +38 (048) 738-57-70 Odessa
  • +38(093) 170-15-42  Life
  • +38 (067) 400-88-44 Kievstar
  • +1(888)393-24-51  USA, Toll free
  • +44(131)507-01-14  Great Britain
  • +7 (499) 348-28-61 Moscow

Bash scripting basics

Bash programming can be thought of as a sequence of commands in the shell that form a specific script of execution. In the context of Bash scripting, we assign tasks to the Bash shell, and script what it should do.

Below we will analyze the basics of Bash scripting and touch on a few special cases of programming in it.

What is Bash Shell?

In IT, a shell is software that connects a user to a computer. The shell allows the user to learn about the system or files in it, or to control the system. The shell is usually part of the operating system. There are two types of casings.

Command-oriented shell such as OS X terminal.

A shell with a graphical user interface, such as a crawler for OS X or the familiar Windows.

Terminal is a command-oriented shell because here we enter commands directly, rather than clicking mouse buttons or entering information into fields. Command-line shells also come in many flavors - Bourne shell, Bash shell, Z shell, and so on. As standard on Mac computers, terminal opens a Bash shell.

How do bash scripts work?

Any Bash script is a simple text file that contains a series of commands for loops and queries. They are a mixture of commands that we would normally enter on the ouselves command line (like ls or cp) and commands that we might enter on the command line that are not there.

When calling a text file as a program, the commands are processed one after the other. Anything we do in the shell can be written into a script, and vice versa, a script can also be copied into the shell and thus executed.

The Bash file can interact with the user through queries, such as "make variables" or "make decisions" by asking "yes" or "no". Through this interaction, scripts can serve a variety of purposes.

Anything you can run normally from the command line can be put into a script and it will do the same. It is generally accepted that files that are Bash scripts have a .sh extension (for example, As you know Linux is a system without extensions, so it is not necessary to have this extension for the script to work.

Choosing a text editor

The text editors preinstalled with your own Linux distribution are mostly ideal for creating Bash scripts. They offer syntax highlighting that highlights commands and variables with color and thus also indicates spelling errors.

Only those who want to create a BASH script under Windows must additionally install a text editor such as "Notepad ++". Command line programs "Nano" and "Vim" also offer syntax highlighting and are almost always available on Linux.

Beginning of work

Select script name and create file

Before writing the filename, we need to make sure it is not already in use. We do this with type.

If the program name does not already exist, type returns "bash: type: program name: not found". Otherwise, the path to an existing file is specified.

Then you can enter standard commands. For example, create a separate directory with mkdir and change it with cd. We also use touch to create an initially empty script file.

mkdir ~ / script files

CD ~ / script files


What do stdin, stdout and stderr mean?

There are three different types of content input and output in Bash, called standard data streams. Short notation is common in the standard. This results in three streams of data stdin, stdout and stderr.

stdin - standard input

Data can be read into programs using standard input. Typically, the standard input is connected to the keyboard. Programs receive user input through manual command recording.

stdout - standard output

Programs can output data to standard output. This is usually done using a monitor. Programs send their output to the user via standard output.

stderr - standard error output

Standard error output is the second output stream that outputs error and status messages. Usually also on the monitor. Standard error output can also be redirected separately from standard output so that error messages do not mix with the output.

As a result, standard data streams can be managed as follows:

  • redirect the stdout data stream to a file;
  • redirect the stderr data stream to a file;
  • redirect from stdout to stderr stream;
  • redirect from stderr to stdout stream;
  • redirect to file using stderr and stdout;
  • redirect with stderr and stdout to the stdout data stream;
  • redirect using stderr and stdout to the stderr data stream;

Two examples:

In the following example, the output of the program — here ls — is written to a file. As a result, the list command writes all files in the current directory to the file myfile.txt.

$ ls > myfile.txt

In the following example, a stream of data from the stderr program - here grep - is written to a file.

$ grep da * 2> grep-errors.txt

Unfortunately, this short article cannot cover all the features of Bash scripting. But now you present the key features of this shell and the principles of working with data in it.

Order hosting and choose a domain in the company "Hosting Ukraine".

We have a high-quality and reliable service, a convenient control system through the admin panel, intelligent security systems and technical support, which will help resolve all emerging issues at any time of the day.

Any tariff of our hosting is suitable for Wordpress. You can choose exactly the package that best suits the needs of your project: SSD hosting, VPS on SSD, Cloud (cloud) hosting, cloud VPS. Dedicated server or dedicated server.

We have developed a system of automatic website testing, this will give you the opportunity to check dozens of indicators and settings of the website and domain. Will check the correctness of the WordPress configuration file, database connection, theme and module settings. Check if the php parameter is configured correctly_memory_limit.

Join Hosting Ukraine and we will take care of the technical side of your business.


Only registered users can leave comments

Subscribe to the newsletter

We will send announcements of new articles and useful tips once a week

Follow us on social networks