赛派号

洗衣机尺寸规格长宽高 Bash (Unix shell)

GNU replacement for the Bourne shell This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages) This article contains instructions or advice. Wikipedia is not a guidebook; please help rewrite the content to make it more encyclopedic or move it to Wikiversity, Wikibooks, or Wikivoyage. (August 2025) This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: "Bash" Unix shell – news · newspapers · books · scholar · JSTOR (August 2025) (Learn how and when to remove this message) This article contains too many or overly lengthy quotations. Please help summarise the quotations. Consider transferring direct quotations to Wikiquote or excerpts to Wikisource. (August 2025) (Learn how and when to remove this message) (Learn how and when to remove this message)

Original author(s)Brian FoxDeveloper(s)Chet RameyInitial release8 June 1989; 36 years ago (8 June 1989)Stable release5.3[1]  / 3 July 2025 Repositorygit.sannah.gnu.org/cgit/bash.git Written inCOperating systemLinuxUnix-likemacOSWindowsAndroidOracle SolarisPlatformGNUAvailable inMultilingual (gettext)TypeShell (computing), Unix shell, command languageLicense v.4.0+: GPL-3.0-or-later[2] v.1.11 – v.3.2: GPL-2.0-or-later[3] v.0.99 – v.1.05: GPL-1.0-or-later[4][5] Websitewww.gnu.org/software/bash/

In computing, Bash is an interactive command interpreter and programming language developed for Unix-like operating systems.[6][7] It is designed as a 100% free alternative for the Bourne shell, `sh`, and other proprietary Unix shells.[8] Bash has gained widespread adoption and is commonly used as the default login shell for numerous Linux distributions.[9]

Created in 1989 by Brian Fox for the GNU Project, it is supported by the Free Software Foundation.[10] Bash (short for "Bourne Again SHell") can operate within a terminal emulator, or text window, where users input commands to execute various tasks.[11][12] It also supports the execution of commands from files, known as shell scripts, facilitating automation.

The Bash command syntax is a superset of the Bourne shell, `sh`, command syntax, from which all basic features of the (Bash) syntax were copied. As a result, Bash can execute the vast majority of Bourne shell scripts without modification. Some other ideas were borrowed from the C shell, `csh`, and its successor `tcsh`, and the Korn Shell, `ksh`. It is ailable on nearly all modern operating systems, making it a versatile tool in various computing environments.

Definitions [edit] ASCII, strings and numbers [edit]

The input language to the shell shall be first recognized at the character level.[13]

— POSIX 1003.1-2024, 2.10.1 Shell Grammar Lexical Conventions

All input and output at the command line is communicated using printable, human-language characters, such as the letter "a" or the number "1."[14] One of the earliest devised and most widespread ways of encoding these characters in a consistent manner which computers could understand was a standard called ASCII.[15] ASCII systematically encodes human readable text into and out of binary code which can be read by computers.[16] Today, all modern terminal emulators are capable of understanding all 95 English language printable characters and numerous control characters from the 128 code-point ASCII standard for character encoding.[17] Control characters most commonly seen during a shell session are "newline," "tab," "space" and "null."[18]

$ printf ': \n' $'\n' : $ printf ': \n' $'\t' : < > $ printf ': \n' " " : < > $ printf ': \n' $'\0' :

Any series of characters is called a "string," or sometimes a "string literal." In Unix-like operating systems, all characters, printable and non-printing, except for a few such as the null character and forward slash /, can be used in naming files. In Unix-like operating systems, all strings are case-sensitive.[19]

In everyday life, people usually begin counting some group of items from the number one: 1, 2, 3 apples. In computer science it is customary to do as the computers do and begin counting the first item from the number zero: 0, 1, 2 oranges, for a total of 3 oranges.

Among computer languages, zero-based numbering was used early on in BCPL in 1967.[20] BCPL is a precursor of the C programming language, in which Bash is written. In C++, a descendant of C, it's also true that "arrays start indexing from element 0."[21] The array's name and index number are a synonym for the data's location in memory.[22] In 1982, E.W. Dijkstra advocated for zero-based numbering of array indices in his paper, "Why numbering should start at zero." As he notes, 'an element's subscript equals the number of elements preceding it in the sequence.[23]

CLI and GUI [edit] Main articles: Computer terminal, VT100, Command-line interface, Terminal emulator, and Graphical user interface

Long after the first Analytical Engine, in the mid-1960's one of the primary ways for humans and computers to interact in real time was at a keyboard with a teleprinter: only display of textual characters was possible.[24] In the 1970s, after computer monitors were developed yet before display of graphics was ailable, these interfaces were integrated and were known as hardware video terminals. Their interaction paradigm is the basis of what we consider the command-line interface (CLI).

When computers gained the ability to draw graphics on a monitor, graphical user interfaces (GUI's) were designed.[25] Terminal emulators are GUI software programs which create a visual representation of these earlier hardware computer terminals. Today, when a human user initiates a login session, that procedure usually occurs via some graphical user interface.

Control+key combinations [edit]

This functionality is provided by a program called GNU Readline and is ailable in interactive mode only. Certain keypress combinations allow a user to operate Bash to use tab completion and to search the command history.

Tab ↹ - Activate tab completion ↑ - Scroll up (ie, backward) in the command history ↓ - Scroll down (ie, forward) in the command history Ctrl+r - Search the command history

Some keypress combinations also allow a user to operate the terminal emulator in order to move the cursor within the terminal window and to control the emulator program. By default, these keypress combinations in Bash mirror those of the Emacs text editing software.[26]

Default keybindings for control codes include:

Ctrl+f - Move the cursor one character to the right Ctrl+b - Move the cursor one character to the left Alt+f - Move the cursor one word to the right Alt+b - Move the cursor one word to the left Ctrl+a - Move the cursor to the beginning of the current commandline Ctrl+c - Cancels the current command and presents a new prompt Ctrl+d - Closes the current Bash instance, possibly also closing the terminal-emulator Ctrl+e - Move the cursor to the end of the current commandline Ctrl+q - Wake the terminal; buffered keypresses are then processed Ctrl+s - Put the terminal to sleep Ctrl+w - Remove one word to the left of the cursor Ctrl+z - Stop a foregrounded process

Vi (text editor) keybindings are also ailable and can be enabled by running set -o vi.[27][28]

Syntax [edit] "Tokens" \_ "Blanks" \_ "Operators" (Ops) \_ "Control Ops" \_ "Redirection Ops" \_ "Words" \_ "Reserved Words" \_ "Names"

syn · tax 1 a : the way in which linguistic elements (such as words) are put together to form constituents (such as phrases or clauses) b : the part of grammar dealing with this[29]

— Merriam-Webster Dictionary

When Bash reads a full commandline, the complete string is broken down according to a certain set of rules into individual units called "tokens." "Tokens" are identified using, and separated from each other using "metacharacters." (As of version 5.3:)

The ten "metacharacters:" ‘space’ ‘tab’ ‘newline’ ‘|’ ‘&’ ‘;’ ‘(’ ‘)’ ‘’

"Blanks" are composed entirely of unquoted metacharacters, "operators" each contain at least one unquoted metacharacter and "words" may not include any unquoted metacharacters.

In practice, Bash breaks down full command strings into tokens or groups of tokens that do contain metacharacters and tokens or groups of tokens that do not contain any metacharacters -- called "words." From there it further breaks words down into more specific, meaningful pieces like command names, variable assignment statements, etc.

The two "blanks" ‘space’ ‘tab’ "Operators" Eight of the twelve "control operators:" ‘newline’ ‘;’ ‘&’ ‘|’ ‘||’ ‘&&’ ‘(’ ‘)’ ... Five of the fourteen "redirection operators:" ‘’ ‘&>’ ‘ : "${zork}" + : cc : dd $

The first word of a commandline is known as the "command position." Under UNIX coventionality, the first word of the commandline is always some kind of a command, and the rest of the words in the commandline string are either options for the command, arguments for the options, or some kind of input upon which the command will operate. "Options" are also called "flags," "switches," or, more formally, "operators." When Bash attempts to locate a command for execution, the directories it searches are those listed in the $PATH variable and the current working directory.[51]

$ # [COMMAND POSITION] [OPTION] [ARGUMENTS] $ # ,--^ ,------------^ ,----^ $ declare -p USER BASH_VERSION declare -x USER="liveuser" declare -- BASH_VERSION="5.2.37(1)-release" $ Users and PS1 [edit]

A user account can be created for either a human or a programmatic user. In Unix-like OS's, there are two kinds of users: "privileged" and "regular." A privileged user, such as "root" or the operating system kernel, is allowed to do anything whatsoever on the machine. Unprivileged users are limited in various ways.

When an interactive shell session waits for user input, often within a terminal emulator connected to a peripheral keyboard, by default it prints a particular string of characters to the screen.[52] In Bash, the value of this waiting-string is held in the shell variable $PS1. For regular users a common default value for $PS1 is the dollar character, $.[c] For the superuser a common default value is hashtag (#)

$ sudo --login --user root [sudo] password for liveuser: # vim /home/liveuser/names.txt # exit $ grep -e bob ./names.txt grep: ./names.txt: Permission denied Modes [edit] Programming paradigm [edit]

Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command. Because it must satisfy both the interactive and programming aspects of command execution, it is a strange language, shaped as much by history as by design.[53]

— Brian W. Kernighan & Rob Pike

Bash was written in C.[54] A modular style can be approximated through good style and careful design.[55] It is often used in an imperative or procedural style.[56]

Interactive and non-interactive modes [edit]

As a command processor, Bash can operate in two modes: interactive or non-interactive. In interactive mode, commands are usually read from a terminal emulator. In non-interactive mode, which facilitates automation, commands are usually read from named files known today as shell scripts. When executed as a standalone command at the command-line interface (CLI), by default Bash opens a new shell in interactive mode.

Scripts [edit]

Shell scripts are text files that contain code, often commands, intended to be read and acted upon by some particular interpreter in a batch process in a non-interactive mode and without any further user interaction. Interpreted scripts are programs that do not require their source code to be compiled: all of the relevant source code is contained within the script. There are many programs which can serve as an script interpreter: Perl,[57] Ruby, Python,[58] AWK, etc. Interpreted scripts are most often written for Unix shells.

The first two characters of the first line of any (executable) shell script begins with a something called a hash-bang: literally the characters hashtag (#) and bang (!) side by side.

$ cat ./example.sh #! /bin/env bash echo foo exit $

If a script is intended to be run by a user as a stand-alone program on the commandline, then it is referred to as an "executable." By convention, the filenames of executable unix shell scripts are identified the suffix .sh. The "execute" bit can be enabled on a shell script with the utility chmod:

$ ls -l ./example.sh -rw-r--r--.1 liveuser liveuser 32 Aug 3 22:33 example.sh $ ./example.sh bash: ./example.sh: Permission denied $ chmod 0744 ./example.sh $ ls -l ./example.sh -rwxr--r--.1 liveuser liveuser 32 Aug 3 22:33 example.sh $ ./example.sh foo $ The source builtin [edit]

With the source, or synonymous . command, Bash reads and executes shell commands from any text file by name.[59]

Login and non-login shells [edit] Main article: Session (computer science)

Bash can be executed as a login shell, or "session leader," in both interactive and non-interactive modes via the --login option. "Logging in" requires user authentication. For this reason, only one login shell exists per user session. In GNU/Linux, a user's login shell is identified in the /etc/passwd file.

$ awk -F ':' '$1 ~ /root/' /etc/passwd root:x:0:0:Super User:/root:/bin/bash

When a human user initiates a login session, this procedure often occurs in a graphical user interface (GUI). When a user opens a terminal emulator, the emulator executes a non-login instance of the user's login shell.

Logging out of a shell session from within a terminal emulator can be accomplished with the exit command or, by default in Bash, pressing Ctrl+d.

Startup source files [edit] Main articles: Unix shell and Hidden file and hidden directory

When Bash starts, it uses source to execute commands in a variety of dotfiles (see lists below).[60] These dotfiles, unlike shell scripts, typically he neither the execute permission enabled nor an hash-bang. By default Bash will source a somewhat different set of files, and in a different sequence, depending on:[61]

How bash is called interactively, non-interactively, invoked with name sh Which options are used --login, --rcfile, --norc, --posix Which environment variables are defined BASH_ENV, ENV , and Which files exist /etc/profile ~/.bash_profile ~/.bash_login ~/.profile ~/.bash_logout, and ~/.bashrc among others.

Of course, any startup file can also execute commands from any other file. Startup files can affect shell behior, terminal emulators, the X window system and the window manager.

POSIX mode [edit] Main articles: POSIX and POSIX terminal interface

The POSIX IEEE 1003.1 standard specifies a common set of definitions that any shell system application (bash, dash, zsh, etc.) may conform to.[62] Any shell user script (./myscript.sh) written in conformance with POSIX guidelines should be executable by any shell system application that has implemented the POSIX specification. As a result, there can be a reasonable expectation that POSIX-compliant scripts can be executed with success on any Unix or Unix-like operating systems which implements the POSIX standard (Linux, OpenBSD, Oracle Linux, HP-UX, etc.). These scripts are considered "portable" as they are and without any further modifications. The portion of POSIX that applies to shells and command line utilities is a subset of a larger group of POSIX standards that further specify how terminals and terminal emulators aught to function in order to also be considered portable.

When Bash is operating in POSIX mode, fewer features are ailable but the resulting code can be executed on a greater variety of operating systems.

To enable POSIX mode at the initialization of an interactive shell, Bash can be executed as either sh, bash --posix or bash -o posix.[63] To cause a script to be initialized in POSIX mode, one would use the either the hashbang #! /bin/env sh or the less portable #!/bin/sh. When an instance of Bash is operating in POSIX mode, the environment variable $POSIXLY_CORRECT is defined, and the value of the environment variable $SHELLOPTS includes the string posix.

$ declare -p POSIXLY_CORRECT bash: declare: POSIXLY_CORRECT: not found $ sh $ declare -p POSIXLY_CORRECT declare -- POSIXLY_CORRECT="y" $

The full list of features ailable in Bash which are not specified by POSIX is considerable.[64] Here is a partial list:

Any arrays other than the array of positional parameters, $@, are not POSIX The double bracket extended test construct, [[...]], is not POSIX [...] and test are POSIX One of the double-parentheses arithmetic-evaluation syntaxes, ((...)), is not POSIX $((...)) is POSIX Brace expansion, kernel{,-headers}, is not POSIX Dynamic scoping of parameters and the local builtin are not POSIX Process substitution, , and appending, >, Here documents, file 2>&1'. Bash supports here documents. Since version 2.05b Bash can redirect standard input (stdin) from a "here string" using the , Here documents,

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了