Bash Regular Expressions: Creation Guide, Application, Examples

Shell Bash regular expressions are designed for one of the main tools that allow interaction between the user and the operating system. Through the shell, the user can manage the files and directories present in the file system of the machine, process their contents and execute other programs using the keyboard of his terminal as an input unit and the alphanumeric screen of the terminal as an output device.

Command language history

Command language history

Bash regular expressions were developed by Brian Fox for the GNU project as an alternative change of software for the Bourne shell. The command language was published in 1989 and spread widely as the default login shell for Linux and MacOS distributions via Apple (formerly OS X). The version is also available for Windows 10 and is the default user shell in Solaris 11.

Bash is an instructional processor that traditionally runs in a text terminal, where a developer runs commands that invoke actions. Bash regular expressions are read and executed from a file called a shell script. Together with Unix, it recognizes file names (wildcard comparisons), protocols, documents, directive expansion, and control structures for testing criteria. In the main words, the syntax and other key language identities are reproduced from csh and ksh. Bash is a POSIX-compatible shell, but with some extensions. The name of the shell is short for Bourne-again shell.

Brian Fox began coding Bash on January 10, 1988 after Richard Stallman was dissatisfied with the lack of progress in developing a free shell that could run existing scripts. Fox released Bash as a beta on June 8, 1989, and remained the main developer of the project from mid-1992 until mid-1994, after which he was fired from the FSF and Chet Rami took his place.

During this period, Bash was the most popular program among Linux users, becoming the default interactive shell in various distributions of this operating system, as well as in MacOS from Apple. Bash has also been interspersed in Microsoft Win with Cygwin, in DOS on the DJGPP project and Android using various terminal emulation applications.

In early September 2014, a decent security breach was discovered in Bash version 1.03, released in August 1989, called Shellshock, which led to a number of attacks via the Internet. The error was considered serious, because CGI scripts using Bash became vulnerable, which allowed the execution of arbitrary code. Patches for fixing errors became available immediately after they were discovered, but not all computers were updated.

Shell Syntax Features

Bash is a superset of the Bourne shell commands and uses bracket expansion, command line termination, basic debugging, and trap exception handling among other functions. The vast majority of Bourne shell scripts run unchanged, with the exception of scripts that are interpreted differently or try to run a system command. Bash grep regular expressions, as well as GNU tools, use a compressed way to scan for software errors and set the exit status, which allows threads to jump to traditional destinations.

If the developer presses the tab button in the shell, Bash automatically applies the end of the command line in order to match the typed names of programs, files and variables. The command line termination system is infinitely flexible and manageable, and it is often composed with functions that store arguments and file names for specific programs and tasks. The Bash syntax has enough extensions that are missing from the Bourne shell.

Bash regular expressions: perform integer-valued calculations of an arithmetic estimate, use the ((...)) command and the $ ((...)) syntax argument to simplify I / O forwarding. For example, it has the ability to redirect output (stdout) and crash (stderr) synchronously with support for the &> operator. The present is easier to introduce than the equivalent of the Bourne shell 'command> file 2> & 1'.

Bash uses a process override with support for Linux regular expression syntax and replaces the output of a command (input) that traditionally uses a file name. When using the keyword “function”, Bash declarations are incompatible with Bourne and Korn scripts, since the Korn shell has the same problem when applying the “function”, but it accepts the same function declaration syntax as the above shells, being POSIX-compatible.

Because of these and other differences, scripts are rarely run under the Bourne and Korn interpreters unless they have been specifically written with this compatibility in mind, which should be considered when planning for working with Bash regular expressions. Associative arrays allow fake support for indexed arrays, similar to AWK. Bash 4.x has not been integrated into the new version of MacOS due to license restrictions. An example of an associative array.

Associative array example

Command Execution Modes

The shell has two modes of command execution: batch and parallel. Commands in batch mode are separated by the “;” symbol. Bash regular expressions example:

  • command1;
  • command2.

In this example, when command 1 is completed, command 2 is executed. And you can also execute background execution of command 1 using (symbol &) at the end of execution, the process will be executed in the background, immediately returning shell control and allowing the user to use executable commands.

Command Execution Modes

For the simultaneous execution of commands 1 and 2, they must be executed in the shell as follows:

  • command1 & command2.

In this case, command 1 is executed in the background & symbol, immediately returning control to the shell, which executes command 2 in the foreground. Bash grep regular expressions can be stopped and returned to control by typing Ctrl + z while the process is running in the foreground. A list of all processes, both in the background and in the stop mode, can be achieved by running jobs.

The state of the process can be changed using various commands. The “fg” command brings the process to the forefront, and the “bg” set stops the process running in the background. Bg "and" fg "can take a job identifier as their first argument to indicate which process to act on. Without this, they use the default process, indicated by a plus sign in the output of jobs. The kill command can be used to terminate the process prematurely by sending it a signal.The job identifier should be indicated after the percent sign:

  • kill -s SIGKILL% 1 or kill -9%.

Bash supplies “conditional execution” to command separators that execute contingent commands according to the exit code set by the use-case command. An external command called "bashbug" reports shell errors. When a command is invoked, it launches the default editor for the user with the completed form. The form is submitted to the Bash parties, or possibly other email addresses, providing a global replacement for Bash regular expressions.

Reserved Bash Variables.

Reserved Bash Variables

Scripts for launching an interactive environment

When Bash starts functioning, it executes various point files. Even for similar script commands that have permission to execute and order the interpreter, for example:

  • #! / bin / bash.
Scripts for launching an interactive environment

The initialization files used by the Bash assignment expression do not require this. The order of execution of files:

  1. When the shell [edit] is started, it reads and executes / etc / profile, if there is one.
  2. This file initiates /etc/bash.bashrc.
  3. After defining this file, it searches for ~ / .bash_profile, reading and executing the 1st, existing and readable.
  4. If the shell follows from [edit], it defines and executes ~ / .bash_logout.
  5. At startup, it defines and executes /etc/bash.bashrc and then ~ / .bashrc as the [edit] shell.
  6. The present has the ability to be disabled through the "--norc" option.
  7. The "--rcfile" file parameter forces Bash to read and execute it.
  8. Mapping to the Bourne shell and csh startup [edit], exit the Bourne shell and csh. They allow you to narrow down the overall use of files with Bourne and allow individual startup functions known to csh users.

Portability of POSIX 1003.2

Portability of POSIX 1003.2

Calling Bash with the –posix option or specifying set -o posix in a script makes the regular expression for escaping Bash very closely match the POSIX 1003.2 standard. Shell scripts intended for portability should at least consider the Bourne shell that it intends to replace. Bash has certain features that the traditional Bourne shell lacks. These include:

  1. Some advanced call options.
  2. Command substitution using the $ () notation. This feature is part of the POSIX 1003.2 standard.
  3. Parenthesis extension.
  4. Some operations with arrays and associative arrays.
  5. Extension of the test design with double brackets.
  6. The arithmetic-evaluative construction of Bash regular expressions in "if".
  7. Some string manipulation operations.
  8. Replacement process.
  9. Regular expression match operator.
  10. Bash-specific embedded coprocesses.

Bash arithmetic expressions use "readline" to provide keyboard shortcuts and edit command lines using default key bindings (Emacs). Vi bindings can be enabled when running "set -o vi".

Expanding Braces with Wildcards

Expanding Braces with Wildcards

Bracket expansion, also called interleaving, is a function that is copied from the C shell. It generates a set of alternative combinations. Generated results do not have to exist as files. The results of each extended row are not sorted and stored in the order on the right. Users should not use parenthesis extensions in portable shell scripts because the Bourne shell does not produce the same output.

When a brace extension is combined with wildcards, the brackets first expand and then the resulting wildcard is replaced. In addition to interleaving, bracket expansion can be used for consecutive ranges between two integers or characters separated by double dots. Newer versions of using Bash regular expressions allow a third integer to specify an increment.

When a bracket extension is combined with a variable extension, it is performed after the bracket extension, which in some cases may require the use of the "eval" built-in, thus:

  • $ start = 1;
  • end = 10 $ echo {$ start .. $ end} # cannot expand due to the evaluation order {1..10};
  • $ eval echo {$ start .. $ end} # the expansion of the variable occurs, then the resulting string is evaluated: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

Syntactic aspects of the Bash language

Syntactic Aspects of the Bash Language

Shell scripts must be stored in an ASCII text file created using the editor program, which does not enter additional characters or sequences to format text. For example, editors suitable for creating shell scripts are vi or Emacs programs available on UNIX / Linux, or programs such as Notepad, TextEdit, and UltraEdit on Microsoft Windows.

It’s good practice to insert the regular line, “#! / Bin / bash ", which shows the absolute path of the program in the file system of the machine on which you want to run the script. Thus, you can run it directly on the command line without specifying the file name as an argument to the bash command.

The instruction of the translator program that the operating system will use to translate and execute script instructions is provided in the first line of the script itself, immediately after the sequence of characters "#!". It is believed that the executable file of the interpreter is located in the “/ bin” directory, but on different systems it can be installed in other directories, for example:

  • “/ Usr / bin”, “/ usr / local / bin”.

In general, the “#” character allows you to enter a comment in the script source. Any character in the script line after the “#" character is ignored by the shell. In fact, it is often used to insert comments into a script source to describe its operation or to explain the impact of specific commands. As when inserting commands in interactive mode, even when coding a script, each program instruction can be written on a separate line or broken into several lines and ends each line, except the last, with the “\” symbol. Additional instructions can be reported on the same line using ";".

Program instructions may be indented to make the source code more readable, but you should pay attention to the use of spaces. The Bash interpreter is more “legible” than other interpreters or compilers, and in some cases it is not allowed to insert arbitrary spaces between the members that make up the instructions, in other cases the use of space is important for the correct interpretation of the instruction.

There are no characters to distinguish between blocks of instructions inserted in the control structure, for example, which must be repeated in the iterative control structure. On the other hand, there are corresponding language keywords that correctly identify the beginning and end of a block. These keywords vary depending on the instruction used to control the flow of the program. In the syntax of examples of "match" Bash regular expressions, some characters take on a special meaning, that is, if they are present in a character string or as an argument to a command, they perform a very precise function.

Interactive launch

Interactive launch

With minimal simplification, we can say that a shell is a program that always interactively performs the same operation. She waits for the command to be entered as input, evaluates it to make sure the command is syntactically correct, and executes it, then returns to wait for the next command. This process ends when the shell receives a signal indicating that the input is complete and no other commands will be sent to it. At this point, the shell program ends, freeing the allocated memory and other machine resources available to the operating system.

The script is started automatically by the operating system when the user logs into the system itself, that is, it can be executed by the user through a command specified on an already open shell, or using special graphical utilities if it works with a system with a graphical user interface. For example, on an Apple Macintosh computer running Mac OS X, you can use the command shell by running the Terminal utility located in the Utility in the Application folder.

On a Linux workstation with a graphical desktop manager, such as GNOME or KDE, you can open a command shell by selecting the Terminal program from the Applications → Accessories menu. After activating the command shell, you can view the name of the shell we use by executing the following commands:

  • $ echo;
  • $ SHELL / bin / bash.

If the default shell is not Bash, you can check whether it is present on the system in one of the directories listed in the PATH environment variable using the “which” command and execute it using the “bash” command:

  • $ echo $ SHELL / bin / tcsh $ which bash / bin / bash $ bash bash-2.03 $.

Thus, the shell works interactively, receiving input to each individual command and parameters specified on the command line, and executing the command itself. The output is displayed in the same terminal window. Each command passed to the shell ends by pressing the Invio / Enter key. You can issue several commands on the same line, separating them from each other with the symbol “;”. It is also possible to split the command insert into two or more lines, ending each intermediate line with a “\”.

Apiks, double and back quotes

Apiks, double and back quotes

Typically, in programming languages, quotation marks and double quotation marks are used to delimit strings, and the use of one or the other character depends on the syntax adopted in a particular language. In scripting languages, using quotes and backreferences has a different meaning, and Bash is no exception.

Single quotes are used to delimit character strings. The interpreter does not enter the contents of the string and simply uses a sequence of characters separated by quotation marks. , , , . , , , - . .

Double quotes are used to delimit strings, however, if the string is limited to this character, the interpreter performs the so-called "interpolation" and resolves the value of any variables in Bash regular expressions in the string. In practice, if a string enclosed in double quotes contains a reference to a variable, then the string name of the variable is replaced by its value. To print characters, such as double quotes or the dollar, which would otherwise be interpreted and take a different value, you must write the prefix of each of them with the backslash “\” character. To print a backslash character in a string enclosed by double quotation marks, you must return two backslashes.

The nature of the return stroke has the most characteristic behavior typical of scripting languages, and is absent in the main high-level programming languages. The quotation mark allows you to delimit the line that Bash interprets as a command and must be executed, returning as the value of the output to the same channel product of the output standard.

Command Examples

Command Examples

If you want to execute the shell in such a way that it processes the sequence of commands shown in the ASCII text file:

  • $ pwd;
  • echo $ SHELL;
  • hostaname / home / marco / bin / bash aquilante $ echo \> $ SHELL / bin / bash.

If you need to prepare a file called “script.sh”, which is stored in the home directory, the contents of the file may be as follows:

  • echo -n "Oggi e 'il" 2 date +% d /% m /% Y.

This very simple script is launched by specifying the file name on the command line with which the shell is called:

  • $ bash script.sh Oggi e 'il 06/10/2011.

The shell can also accept a sequence of commands to execute through a channel that redirects the output of another command to standard Bash input:

  • $ cat script.sh | bash Oggi e 'il 06/10/2011.

You can select a regular expression line of the Bash program with the designation “#!”. The absolute path of the interpreter that will be used to execute the script is launched directly without the OS, by running Bash and passing the script to the input:

  • $ cat script.sh #! / bin / bash echo -n "Oggi e 'il" date +% d /% m /% Y $ chmod 755 script.sh $ ls -l script.sh -rwxr-xr-x 1 marco users 49 18 Apr 23:58 script.sh $ ./script.sh Oggi e 'il 10/6/2011.

In the last command of the previous example, which directly calls the execution of the script stored in the file “script.sh” present in the current directory, the relative path “./” to the file name is indicated. You must specify the path to the directory where the executable script is located, because often for security reasons the current directory is not in the list of directories in which the shell should look for external executable commands. A list of such directories is stored in Bash variable regular expressions.

Benefits of an Operating System with Bash

This is the most efficient shell scripting language. It gives the user a simple way to automate work if he is already familiar with using the shell interactively. If a developer is programming systems, then he needs to know how the shell works.

If you compare the scripts with the study of the configuration or automation system "yaml" or "json", they are much more versatile. Bash scripts are simpler because the script works by default.

Bash is a simpler language, and this forces developers to focus on other complexities of the system. Bash works great for shell writing. Everything else basically either uses a shell for commands, or implements its own shell, copying the good parts from it. In addition, there are good Bash regular expression constructors that greatly simplify shell handling.

With Bash, developers can leverage interactive web experience by leveraging Linux command-line experience without the boundaries of time and place. Using this feature does not require strict rules and efforts, and users can access the authenticated workstation by managing resources and the Azure environment with one click, even when they use the Azure, Azure Portal and Azure Documentation mobile applications.

Unlike a traditional command-line environment, there is no need to install and select tools before starting work, and you can save time and effort with Bash. All CLI tools such as text, assemblies, containers, and source are available in Bash, and you can use secure and simple tool authentication with CLI 2.0.

We looked at examples of Bash regular expressions. Good luck in mastering!


All Articles