Bash

Bash is a command shell for UNIX-like operating systems (UNIX, GNU/Linux, macOS). It provides the user with a command system for working with files and folders, searching, and configuring the environment, and allows OS management directly from the command line.

TRTY

                                        Bash logo

The word bash is pronounced “bash” and stands for Bourne-Again Shell. Stephen Bourne is the first and last name of the shell’s creator, and sh (which stands for “shell”) is its previous version. It was used in older versions of UNIX operating systems. Almost all modern command systems for working with OSs are based on the shell.

What does bash do?

The shell accepts commands entered by the user at the command line and interprets them, translating them into machine code. The operating system receives this code as instructions and executes them.

Another way to use it is to create bash or shell scripts saved in a file. Each time the file is run, the specified commands will be executed. 

Why use bash?

The same actions with files, folders, and search can be performed using the OS’s graphical interface. However, this is slower, more inconvenient, and more complex. Programmers use bash or shell to simplify and speed up working with the system.

For example, to copy a file using the graphical interface, you need to open the folder where it’s located, right-click the file, open the context menu, and select “Copy.” However, if you use the command line and bash, you only need to enter a single command.

cp <file1> <file2>

The cp command means “copy.” <file1> is the path to the source file, for example,/home/file.txt. This means that file.txt is located in the /home folder.

<file2> is the path to the copy of the file that will be created when the command is executed. For example, /home/usr/file2.txt, where file2.txt is the name of the copy. It will be located in the /usr folder within the /home directory.

Some systems use little or no graphical interface. Everything must be done through the command line. Bash/shell is indispensable here.

How to run bash

The shell interpreter is built into the operating system and turns on automatically. Simply open a terminal window and start entering commands.

TTRE

Launching bash

To run a bash script written to a file, you need to grant it execution rights.

Team structure

The command syntax looks like this:

<command> [flags][arguments]

Flags are short input parameters after a command, typically consisting of a hyphen and a letter. For example, in ls -l, -l is a flag. This is used to cause the command to execute in a specific way, such as displaying a specific output. There are dozens of flags for most commands.

Arguments are the data passed to a command. For example, to create a file, you need to specify its name—it will be an argument.

Flags and arguments are not always needed, and not for all commands.

How bash navigation works

File and folder organization in Linux is different from that in Windows. File paths are calculated not from the drive letter, but from the so-called root directory. In “beginner” terms, this is the starting point: all files and folders in the system are considered to be located within the root directory. These folders may contain subdirectories and files, but their locations are also relative to the root directory. The root directory is denoted by the forward slash /.

To point to a file in the root directory, use /<file_name>. The full path to any file can be written like this: /<folder1>/<folder2>/…/<foldern>/<file>.

You don’t have to specify the entire path. You can specify the location of files and folders relative to your current location.

In Windows systems, backslashes \ are used to specify the path. In Linux, they are always forward slashes /.

Commands for navigating the system

Constantly typing out the entire path is difficult and time-consuming. That’s why bash has a notation system specifically for navigation:

  • . — current folder;
  • .. — folder one level higher than the current one;
  • ~ — the home directory. Its default location depends on the operating system distribution. For example, in Ubuntu, it is /home/<username>;
  • — — previous directory.

These notations can be used in place of part of a path or the entire path. For example, you can write ~/myfiles or ../docs/work.

The following commands help you navigate the system and navigate your location:

  • pwd — show the full path to the folder where the user is currently located;
  • cd <path> — change to another directory.

Commands for viewing information

While working, you may need to view a file or manual, or check the contents of a directory. Bash has many options for this, but we’ll cover the main ones for now:

  • man <command> — opens the user manual for a command. This reference is very useful in the initial stages of learning the shell. It contains detailed descriptions of commands.
  • cat <file> — display the contents of a file directly in the terminal. You can pass multiple files to the command, and it will display the contents of each.
  • ls <folder path> — display the contents of a directory. If you don’t pass a path to this command, it will display the contents of the current folder.

Less. The less <file> command opens the given file for reading in a special interface. It’s well-suited for viewing long files because the less interface allows you to navigate the contents:

  • pressing g takes the user to the beginning of the file;
  • pressing G moves to the end;
  • / opens search by content;
  • q exists less.
The bash language depends on the keyboard layout: if you enter g  when the Russian language is enabled, the command will not work.

Nano is a text editor that can be used from the console. It’s very simple: it’s a Notepad-like program that opens directly from the command line. To open a file, enter the command nano <file>. Within the program, you can read, edit, and save its contents. The editor’s basic commands are listed within its interface.

To switch to nano without opening a file, you can run the command without arguments.

Search commands

A user may need to find a file or specific information within it. Two programs are used for this:

  • find <folder> -name “file name in quotes” searches for a file with the required name in the specified folder;
  • grep “any quoted text” <filename> searches for the specified string in a file. This is useful for searching for specific content, such as lines in scripts. If you run grep with the -r argument and specify a folder name, it will search for the specified string in all files within the folder.

Working with files

You can work with everything in the system. Bash provides a huge variety of commands for this. Here are a few:

  • mkdir <folder_name> — creates a new folder in the current directory. To create the folder in a different location, use the mkdir -p <path> command.
  • touch <file> — creates a file. You can specify a path to it, or you can create it in the current directory. In this case, you only need to specify the file name.
  • cp <path1><path2> — copies a file from path1 to path2;
  • mv <path1><path2> — moves a file from path1 to path2;
  • rm — deletes a file, rm -r — deletes a directory;
  • wget <link> — downloads a file from a website via a link and places it in the current directory;
  • echo <text or variable> — outputs the argument to the console.

Input and output

Input and output streams can be redirected using the > and < symbols. This means a command can take arguments not from the console, but from another location, most often a file. It can also output its results not to the screen, but to a file or somewhere else:

  • <command> < <file> — entering arguments from a file;
  • <command> > <file> — output the result to a file;
  • <command> >> <file> — outputs the result to a file with appending. If the document already contained content, it won’t be overwritten—the new data will be appended.

You can run multiple commands at once so they pass data to each other—this is called a pipeline. In this case, the commands are listed separated by |.

Working with git

Popular Linux distributions (Ubuntu, Debian, and others) support the git version control system. 

Bash scripts

If the user needs to perform multiple routine actions in sequence and this algorithm will be used more than once, scripts can be created. Commands are written to a file and executed each time the file is executed. Scripts can be used to automate system operations and simplify processes.

To run a script from the console, you need to enter the command bash <script name> or sh <script name>.

Script start. For the system to recognize a file as a script, its first line should be:

#!/ bin /bash

After the !, the path to the bash interpreter is written, which by default looks as specified in the code. If the path is different on the system, the entry at the beginning of the script will change accordingly. You can find this path using the following command in the terminal:

whereis bash

The script file extension is traditionally specified as .sh. In reality, the system can execute a file with a different extension, or even without one. The .sh extension is primarily used for user convenience, to make it easier for users to recognize the file as a Bash script.

Variables. The scripting language supports variables. They have no type and are declared as follows:

<variable_name> = <value>

The value can be numeric, text, or other. It can be retrieved externally using special notations:

  • $0 — passes the name of the script;
  • $1, $2, and so on are arguments that the user can pass to the script when opening it.

You can access the created variable within the script as follows: $<variable_name>. It is used for calculations, input/output, and other actions.

Conditions and loops. A script can perform actions if a certain condition is met. This is done using the following construct:

If <condition>
then <action if condition is met>
else <action if condition is not met>
fi

Loops allow commands to be executed a specified number of times or while a certain condition is true. This can be used to manage parent and child processes, control versions, or work with a server. 


Explore More IT Terms

RELATED ARTICLES

Leave a Reply

Your email address will not be published. Required fields are marked *