Linux Basics: Introduction to bash. The Bash Shell: An Introduction. Learning bash commands on Linux

On most Linux operating systems, bash is the default command line interpreter. To determine the current interpreter, enter the following command:

$ echo $SHELL
/bin/bash

This example usescommand line interpreter bash. There are many other command interpreters. To activate another interpreter, you can enter its name at the command line (ksh, tcsh, csh, sh, bash, etc.).

Most full-featured Linux operating systems provide all the command interpreters described here. But on smaller operating systems one or two interpreters may be provided.

Checking the availability of the item you are interested ininterpretercommand line

The most in a simple way To check the availability of the command interpreter of interest, enter its name on the command line. If the interpreter has started, it is available.

There are several reasons for choosing alternative shells.

  • Experience working on UNIX System V systems (many of which use ksh by default) or Sun Microsystems and other Berkeley UNIX systems (which often use csh). In this case, the use of these interpreterscommand linewill be more convenient.
  • The need to run scripts that were created for a specific command interpreter.
  • The capabilities of an alternative command interpreter may be preferable. For example, one of the group members Linux users prefers to use ksh because he doesn't like the way bash creates aliases.

Although most users prefer to work in one shell, the skills gained help them learn to work in others.interpreters command line , occasionally looking at the help manual (for example, using the manbash command). Most people use bash because there is no reason to switch to another interpreter.

Using Bash and ch

The name bash is an acronym for Bourne Again SHell. This abbreviation indicates that bash inherits the Bourneshell interpreter (sh command), created by Steve Bourne of AT&TBell Labs. Brian Fox of the Free Software Foundation created bash as part of the GNU Project. Subsequently, development passed into the hands of Chet Ramey at Case Western Reserve University.

The bash interpreter contains features that were developed for the sh and ksh interpreters in early versions of UNIX, as well as a number of features of the csh interpreter.

You can expect that on most Linux systems, except for specialized ones (such as those that are embedded or booted from an external drive), the bash interpreter is used by default. In specialized systems, a smaller command interpreter may be required, which also implies limited capabilities.

Most of the examples in thisarticledesigned for bash use.

The bash interpreter can run in various compatibility modes, simulating the behavior of other shells. It can mimic the behavior of the Bourne shell (sh) or a POSK standards-compliant shell (bash -posix).

As a result, bash is more likely to be able to successfully read configuration files and run scripts written specifically for other shells.

All provided Linux operating systems use the bash interpreter by default, except operating systems booted from removable media, which use the ash interpreter by default.

Using tcsh (and the earlier csh interpreter)

The tcsh command interpreter is an open source implementation of the Cshell (csh) interpreter. The csh interpreter was created by Bill Joy and is used by default on almost all Berkeley UNIX systems (such systems were distributed by Sun Microsystems).

Many of csh's features, such as command line editing and command history management methods, have been implemented in tcsh and other shells. Just as the sh command runs the bash interpreter in sh-compatible mode, the csh command runs the tcsh interpreter in csh-compatible mode.

Using the ash interpreter

The ash interpreter is a lightweight version of the Berkeley UNIX sh interpreter. It doesn't contain many basic functions and does not provide features such as command history.

The ash interpreter is well suited for use in embedded systems with limited system resources. IN operating system FedoraCore 4's ash interpreter is an order of magnitude smaller than the bash interpreter.

Using zsh

The zsh interpreter is another clone of the sh interpreter. It meets the requirements POSIX standard(like bash), but has other features, including spell checking and a different approach to command line editing. The zsh interpreter was the default in early operating systems. MacOS systems X, but in modern systems The default interpreter is bash.

Bash is a command shell or, in other words, a command interpreter that is used by default in most Linux distributions. The Bash shell has a huge amount opportunities. More than just an interpreter, in the right hands Bash can become a full-fledged programming language.

The shell's main job is to execute commands, but in addition to utilities located on the file system, Bash has its own set of commands, many of which you use every day without realizing it. These Bash commands are not on disk, but are built into the shell itself. In this article, we'll look at basic bash commands in Linux that can be useful in some situations.

This article will not have a very long introduction and we will get straight to the point. All list elements are arranged in random order. After going through all of them, you will have a better understanding of working in Bash.

1. source

This command allows you to read and execute all commands from the specified file in the current command shell. This means that all environment variables set in it will be visible in all scripts or commands that you execute. Let me remind you that if you run a regular script and declare a variable in it, then it will be available only to it because a separate shell instance is created for scripts. It inherits the parent's variables, but not vice versa. This command is often used to update the environment after chroot:

source /etc/profile

2. alias

The alias command allows you to create aliases for other commands. For example, you can configure it to run sudo apt update && sudo apt upgrade when you type update. If you call the command without parameters, it will display a list of already defined aliases. See more details about how to create in a separate article.

3.bind

With this command you can associate a keystroke with a specific command or function. For example, let's configure the pwd command to be executed when pressing F12 in a terminal window:

bind ""\e

Options

-c string If this parameter is present, the command will be read from the line string. If the command passed has additional arguments, they are considered positional and start at $0. (Chapter Arguments )
-i Interactive work of the interpreter
-l Watch INVOCATION below
-r Limitations in the operation of the shell. Watch RESTRICTED SHELL
-s If this parameter is specified and all required arguments are missing, they are read from standard input
-D List of all strings enclosed in double quotes with $ at the beginning are output to standard output. Required to complete the translation. No commands will be executed
[-+]0[shopt_option]

shopt_option - Extra options shells. Read the SHELL BUILTIN COMMANDS subsection. If a given parameter is present6 then -0 before it sets the specified parameter, and +0 disables it. If the shopt_option parameter is not specified, then +0 prints a list of installed options to standard output in a format that allows reuse as input.

-- Signals the end of parameter setting. Any parameters after it are treated as filenames and arguments to the script. The "-" option is equivalent to "--".

Bash also allows multi-character options. They must be described before single-character ones in order for them to be recognized correctly.

--debugger Starts the debugger profile before running the script. Enables advanced debugging mode (more details in the description extdebug) and runs the trace function
(see parameter description -o).
--dump-po-string Equivalent to -D, but the output is in localizations.po file format
--dump-strings Completely similar to -D
--help Clue
--init-file file
--rcfile file If the interpreter is launched in interactive mode, it executes the initialization commands of the specified file file, and not from the standard initialization file ~/.bashrc.
--login Same as -l
--noediting
--noprofile Do not read system-wide configuration files when the interpreter shell starts /etc/profile and any local settings files such as ~/.bash_profile, ~/.bash_login or ~/.profile . By default, bash uses them when running in login mode (more details in the INVOCATION parameter).
--norc Do not read or execute the file personal settings~/.bashrc when running interactively. By default, this parameter is set when the shell is started with the command sh.
--posix Change bash's default behavior when encountering a non-POSIX command (to ensure compatibility)
--restricted Shell actions are limited (read below about the RESTRICTED SHELL parameter)
--verbose Same as -v (verbose output)
--version Print information about the current version of the interpreter to standard output

Arguments

If, after processing the parameters, there are still arguments not associated with the -c or -s parameters, then the first argument is considered to be the name of the file containing the script with the commands. $0 is the file name and the remaining arguments are written positionally. Bash reads the script's commands, executes them, and then exits. The exit status will be equal to the exit status of the last script command executed. If no commands were executed, the return value will be "0". First, an attempt is made to open a script from the current folder; if it is not there, the specified file will be searched according to the PATH variable for scripts.

bash call

login shell- if the first character after -- is 0 or the launch was performed with the -login parameter.

interactive shell-interactive work. If launched without arguments and without the -c parameter with the standard input/output device in the form terminal.app or launched with the -i parameter.

Processing initialization files. If any of the initialization parameter files exist but cannot be read, bash on Mac reports an error.

When bash is started in interactive or login mode, the commands from the file are read and executed first /etc/profile (if available), then the presence of files is checked ~/.bash_profile , ~/.bash_login , and ~/.profile and if they are available and accessible, then the commands from them are executed in the same order as they are listed. To skip the processing of the above files, you can use the parameter --noprofile

If bash was started in login mode, the file is checked and, if present, executed ~/.bash_logout .

If launched in interactive mode, without login mode, then during startup the file is additionally executed~/.bashrc . To turn off of this file parameter is used --norc or --rcfile file for replacement executable file another ( file).

When bash is started in normal mode, for example, to execute a script, it looks at the global environment variable BASH_ENV and uses its value to find the executable file.

When bash is called by the command sh he tries to use the most old version to ensure compatibility with the POSIX standard. When called in interactive mode or in --login mode, the files are read and executed sequentially first/etc/profile And ~/.profile , to prohibit the use of these files, use the parameter --noprofile. It also interactively reads the value of the ENV variable to use as a path to find the executable file. When run with sh, the --rcfile startup option is ignored and cannot be used. When launched in standard mode, no configuration files are used; the interpreter operates in POSIX mode.

When bash is running in posix mode using a command line option --posix the interpreter will adhere to the POSIX standard for initialization files. In this case, interactive mode uses the value of the ENV variable, which must contain the name of the initialization file; no other initialization files will be executed.