Bash

ImprimirCitar

GNU Bash or just Bash (Bourne-again sh ell) is a popular command line user interface, specifically a Unix shell; as well as a scripting language. Bash was originally written by Brian Fox for the GNU operating system, and was intended to be the free software replacement for the Bourne shell. First released in 1989, it has been widely used as the login interpreter ( login) default for most GNU/Linux distributions, and also Apple's Mac OS X up to version 10.15. A version is also available for Windows 10 and Android. It is also the default user shell in Solaris 11.

Bash is a shell that usually runs in a text window where the user types commands in text mode. Bash can also read and execute commands from a file, called a script or 'script'. Like all Unix shells, it supports file name grouping (wildcard matching), pipes, here documents, command substitution, variables, and control structures for condition and iteration tests. Reserved words, syntax, dynamic scope variables, and other basic language features are copied from sh. Other features, eg history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with several extensions.

The shell name is an acronym for 'Bourne-again shell' (Bourne shell, again), a pun on the name of the Bourne shell it replaces and the notion of being "born again" (born-again).

A security hole in Bash dating from version 1.03 (August 1989), dubbed Shellshock, was discovered in early September 2014 and received widespread media attention. It quickly sparked a series of cyber attacks on the Internet. Patches to fix the bugs were made available shortly after the bugs were identified.

History

Brian Fox began coding Bash on January 10, 1988 after Richard Stallman became dissatisfied with the lack of progress from a previous developer. Stallman and the Free Software Foundation (FSF) considered a free interpreter that could running existing shell scripts so strategic to a completely free system built from BSD and GNU code that this was one of the few projects they funded themselves, with Fox undertaking the work as an FSF employee. Fox released Bash as beta, version.99, on June 8, 1989 and remained the primary contributor until mid-1992 and mid-1994, when he was fired from the FSF and his responsibility transferred to another early contributor, Chet Ramey..

Since then, Bash has become the most popular shell among users of GNU/Linux distributions, becoming the default interactive shell in the various GNU/Linux distributions (with distributions referring to any operating system that is referred to as a kernel). have the linux kernel) (although the Almquist shell may be the default script shell) and in versions of Apple's MacOS prior to Catalina in October 2019. Bash has also been ported to Microsoft Windows and distributed with Cygwin and MinGW, to DOS by the DJGPP project, to Novell NetWare, and to Android via various terminal emulation applications.

In September 2014, Stéphane Chazelas, a Unix/Linux specialist, discovered a security bug in the program. The bug, first disclosed on September 24, was named Shellshock and assigned the numbers CVE-2014-6271, CVE-2014-6277, and CVE-2014-7169. The bug was considered serious, as CGI scripts using Bash could be vulnerable, allowing arbitrary code execution. The bug was related to the way Bash passes function definitions to subshells via environment variables.

Bash Syntax

The Bash command syntax is a superset of commands based on the Bourne shell syntax. The definitive specification of the Bash command syntax can be found in the Bash Reference Manual distributed by the GNU project. This section highlights some of its unique features.

Most Bourne shell scripts ('shell scripts) can be executed by Bash without any changes, with the exception of those Bourne shell scripts that reference to special Bourne variables or using a Bourne builtin. The Bash command syntax includes ideas borrowed from the Korn shell (ksh) and C shell (csh) interpreters, such as command line editing, command history, directory stack, $RANDOM variables and $PPID, and the POSIX command substitution syntax: $(...). When used as an interactive shell, Bash provides autocompletion of program names, file names, variable names, etc., when the user presses the TAB key.

The Bash syntax has many extensions that are not provided by the Bourne interpreter. Several of the mentioned extensions are listed below.

Access to arguments

Bash scripts receive the arguments you pass to the interpreter as $1, $2, …, $n. You can get the total number of arguments with the $# symbol.

Using $# it is possible to check the number of arguments passed to the script before doing anything with them:

 if [chuckles] $# -lt 2 ]; then I "You need to pass two arguments." Success 1 fi

Another way to access the arguments is through the array $@, whereby you can iterate over all the given arguments:

 for arg in "$@" do I "$arg" gift

Mathematical operations with integers

A major limitation of the Bourne interpreter is that it cannot perform integer calculations without launching an external process. In contrast, a Bash process can perform integer calculations using the ((...)) command and the $[...] variable syntax as follows:

 VAR=55 # Attend the entire value 55 to the variable VAR. (VAR = VAR + 1) # Add one to the VAR variable. Note the absence of the '$' character. (++VAR) # Another way to add one to VAR. Pre-increment style C. (VAR++) # Another way to add one to VAR. C-style post-increment. I $[chuckles]VAR * 22] # Multiply the VAR variable by 22 and replace the order by the result. I $(VAR  22) # Another way to do the same.

The ((...)) command can also be used in conditional statements, since its return code is 0 or 1 depending on whether the condition is true or false:

 if (VAR  And... 3 + X * 2) then I Yeah.
 fi (Z ▪ 23) " fake " I Yeah.

The ((...)) command supports the following relational operators: '==', ' !=', '>', '<', '>=', and '<='.

A Bash process cannot perform floating point calculations. The only Unix shells capable of this are the Korn Shell (1993 version) and zsh (as of version 4.0).

Input/Output Redirects

Bash's syntax allows for different forms of input/output redirection that the traditional Bourne shell lacks. Bash can redirect standard output and standard error streams at the same time using the syntax:

 order ▪" file

which is simpler than typing the equivalent Bourne command, "command > file 2>&1". Since version 2.05b, Bash can redirect stdin from a string using the following syntax (called "here strings"):

 order ; "chain to read as standard input"

If the string contains whitespace, quotes must be used.

Example: Redirects standard output to a file, writes data, closes the file, and resets stdout

 # makes the file descriptor 6 a copy of stdout (file descriptor 1) exec 6"1 # opens the "test.data" file for writing exec 1test.data
 # generates some content I "data:data:data" # recovers original stdout, making it a copy of file descriptor 6 exec 1"6 # closes the file descriptor 6 exec 6"-

Open and close files

 # opens the test.data file for reading exec 6≥test.data
 # read to the end of the file while read - Wow. 6 dta
 do I "$dta" gift ♪ closes the test.data file exec 6."-

Capture the output of external commands

 # Runs 'end' and saves the results in VAR # Finds file names that end with the letter "h" VAR=$-name )

Regular Expressions

Bash 3.0 processes support regular expression matching using the following syntax, reminiscent of Perl:

[ string =~ regex]]

The regular expression syntax is the same as documented in the regex(3) man page. The exit status of the above command is 0 if the string matches the regular expression, and 1 if it doesn't. Parts delimited by parentheses can be accessed in regular expressions, using the BASH_REMATCH shell variable, as follows:

 if [ foobarbletch =~ 'foo(bar)bl(.*)']] then I The regex matches!
 I $BASH_REMATCH -- outputs: foobarbletch
 I $BASH_REMATCH[1]! -- outputs: bar
 I $BASH_REMATCH[2]! -- outputs: etch
 fi

This syntax provides better performance than launching a separate process to execute a grep command, because regular expression matching takes place in the Bash process itself. If the regular expression or string contains a whitespace or interpreter metacharacter (such as '*' or '?&# 39;), must be enclosed in quotation marks.

Backslash escapement

Words of the form $'string' are treated in a special way. These words are expanded to string, with backslash-escaped characters replaced as specified by the C programming language. Backslash escape sequences are decoded as follows:

Escapes with counterbar
Backslash
Escape
It expands to...
bA character of regression
eA character of escape
fA line feeding character (form feed)
nA new line character
rA car return character
tA horizontal tabulator
vA vertical tabulator
A contrabarred character
'A simple quote character
nnnThe character of 8 bits whose value is the number octal nnn (from one to three digits)
xHThe 8-bit character whose value is the hexadecimal number HH (one or two hexadecimal digits)
cxA control-X character

The expanded result is enclosed in single quotes, as if the $ sign were not present.

A double quoted string preceded by a $ sign ($"...") will be translated according to the current locale. If it were C or POSIX, the $ symbol is ignored. If the string is translated and replaced, the replacement will be enclosed in double quotes.

Bash Startup Scripts

When Bash starts, it runs the commands found in different scripts.

When Bash is invoked as an interactive shell for login (login shell), or as a non-interactive shell with the --login option, it first reads and executes commands from the /etc/profile file, if it exists. Then, look for ~/.bash_profile, ~/.bash_login, and ~/.profile, in this order, and read and execute the commands from the first that exists and is readable. The --noprofile option can be used when starting a new shell to inhibit this behavior.

When a shell session ends, Bash reads and executes the commands in ~/.bash_logout, if it exists.

When an interactive interpreter that is not a login shell starts, Bash reads and executes commands from ~/.bashrc, if it exists. This can be avoided by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.

When Bash starts in a non-interactive mode, for example to run a different console script, it looks for the environment variable BASH_ENV, expands its value if it exists, and uses it as the file name to read and execute. Bash behaves as if the following command were executed:

 if [chuckles] - "$BASH_ENV" ]; then. "$BASH_ENV"; fi

but the value of the PATH variable is not used to find the file.

If Bash is invoked with the name sh, it attempts to replicate the behavior of older versions of sh, while maintaining compliance with the POSIX standard. When invoked as an interactive login, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in this order. The --noprofile option can be used to prevent this behavior.

When invoked as an interactive login with the name sh, Bash looks up the ENV variable, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other boot files, the --rcfile option has no effect. A non-interactive shell called with the name sh does not attempt to read any other startup files. When invoked as sh, Bash enters posix mode after reading the startup files.

When Bash is started in POSIX mode, for example with the --posix option, it follows the POSIX standard for startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the value of the expanded variable. No other boot files are read.

Bash attempts to determine when it is being executed by a remote shell daemon, usually rshd. If Bash determines that it is being run by rshd, it reads and executes commands from ~/.bashrc, if this file exists and is readable. It will not do this if called as sh. The --norc option can be used to prevent this behavior, and the --rcfile option can be used to force reading another file, but rshd it normally does not invoke the shell with these options or allow them to be specified.

Bashism

The use of Bash features that are not covered by the POSIX specifications for shells is called bashism. In general, it is recommended to avoid them, to allow porting of scripts to other operating systems.

Contenido relacionado

MediaWiki:Sitetitle

...

World Wide Web

In computing, the World Wide Web or worldwide computer network is a system that works through the Internet, by which various types of data can be transmitted...

Memo pad

Notepad is a text editor included in Microsoft operating systems since 1985.. Its functionality is very simple. Some of its own characteristics...
Más resultados...
Tamaño del texto:
Copiar