Bash

I want to learn more about bash (Unix shell).

Date Created:
2 470

References



Related


  • Shell Program
    • A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be command languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.
  • Command Language
    • A command language is a language for job control in computing. It is a domain specific and interpreted language; common examples of a command language are shell or batch programming languages. These languages can be used directly at the command line, but can also automate tasks that would normally be performed manually at the command line. They share this domain with scripting languages, though a command language typically has a stronger coupling to the underlying operating system. Command languages often have either very simple grammars or syntaxes very close to natural language, to steepen the learning curve.
  • GNU Project
    • The GNU Project is a free software, mass collaboration project announced by Richard Stallman in 1983. Its goal is to give computer users freedom and control their computers and computing devices by collaboratively developing and publishing software that gives everyone the rights to freely run the software, copy and distribute it, study it, and modify it. The founding goal of the project was to build a free operating system.
  • dot file
    • In Unix-like operating systems, any file or folder that starts with a dot character, commonly called a dot file or dotfile, is to be treated as hidden - that is, the ls command does not display them unless the -a or -A flags (ls -a or ls -A ) are used. In most command-line shells, wildcards will not match files whose name starts with . unless the wildcard itself starts with an explicit ..
  • interpreter directive
    • An interpreter directive is a computer language construct, that on some systems is better described as an aspect of the system's executable format, that is used to control which interpreter parses and interprets the instructions in a computer program. In Unix, Linux and many other Unix-like operating systems, the first two bytes in a file can be the characters #!, which constitute a magic number often referred to as shebang, prefix the first line a script.
  • background execution
    • A background process is a computer process that runs behind the scenes (in the background) and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification. On a Unix or Unix-like system, a background process or job can be further identified as one whose process group ID differes from its terminal group ID (TGID). The TGID of a process is the process ID of the process group leader that opened the terminal, which is typically the login shell. The TGID identifies the control terminal of the process group.) This type of process is unable to receive keyboard signals from its parent terminal, and typically will not send output to that terminal.
  • process state
    • In a multitasking computer system, process may occupy a variety of states. These distinct states may not be recognized as such by the operating system kernel. However, they are useful abstraction for the understanding of processes.

Process State

  • signal
    • Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIZ-compliant operating system.
  • Command Shell
    • A command shell is a command-line interface to interact with and manipulate a computer's operating system.
  • Command-line interface
    • A command line interface (cli) is a means of interacting with a computer program by inputting lines of text called command-lines. Command line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user-friendly alternative to the non-interactive interface available with punched cards. Alternatives to CLIs include GUIs and keyboard shortcuts.


Notes


Bash, short for Bourne-Again SHell, is a shell program and command language supported by the Free Software Foundation and first developed by the GNU project by Brian Fox. Designed as a 100% free software alternative to the Bourne shell, it was initially released in 1989. Its moniker is a play on words, referencing both its predecessor, the Bourne shell, and the concept of rebirth.

Bash has gained widespread adoption since its inception and is commonly used as the default login shell for numerous Linux distributions. It holds historical significance as one of the earliest programs ported to Linux, alongside the GNU compiler. It is available on nearly all operating systems. As a command processor, Bash operates within a text window where users input commands to execute various tasks. It also supports the execution of commands from files, known as shell scripts, facilitating automation. Bash incorporates a rich set of features. The keywords, syntax, dynamically scoped variables and other basic features are all copied from the Bourne shell.

Brian Fox began coding Bash in 1988 and the first beta was released in 1989. Bash has become by far the most popular shell among users of Linux, becoming the default interactive shell on that operating system's various distributions.

Features

Brace Expansion

$ echo a{p,c,d,b}e
ape ace ade abe
$ ls *.{jpg,jpeg,png} # expands to *.jpg *.jpeg *.png - after which the wildcards are processed
$ echo {1..10} # Double dots for sequntial ranges (try echo {a..z})
1 2 3 4 5 6 7 8 9 10
$ echo {01..10}
01 02 03 04 05 06 07 08 09 10

When bash starts, it executes the commands in a variety of dot files. dot files do typically have nether the execute permission enabled nor an interpreter directive like #!/bin/bash

Some versions of Unix and Linux contain Bash system startup scripts, generally under the /etc directory. Bash executes these files as part of its standard initialization, but other startup files can read them in a different order than the documented Bash startup sequence.

The Bash shell has two modes of execution: batch (asynchronous) and concurrent (synchronous).

To execute commands in batch mode (i.e., in sequence) they must be separated by the character ; or on separate lines:

$ command1; command2
$ command3

A background execution of command1 can occur using (symbol & ) at the end of an execution argument, and process will be executed in background while immediately returning control of the shell and allowing continued execution of commands.

$ command1 & # Background execution
$ command1 & command2 # Concurrent execution of command1 and command 2

A process can be stopped and control returned to bash by typing CTRL + Z while the process is running in the foreground. A list of all processes, both in the background and stopped, can be achieved by running jobs:

$ jobs 
[1]- Running command1 &
[2]+ Stopped command2

The numbers in the brackets refer to the job id. The plus sign signifies the process for g` and fg. The text Running and Stopped refer to the process state. The last string is the command that started the process. The state of the process can be changed using various commands. The fg command brings a process to the foreground, while the bg sets a stopped process running in the background. bg and fg can take a job id as their first argument, to specify the process to act on. The kill command can be used to end a process prematurely, by sending it a signal. The job id must be specified after the percent sign:

$ kill $1

Bash supplies a conditional expression command separators that make execution of a command contingent on the exit code set by a precedent command.

$ cd "$SOMEWHERE" && ./do_something || echo "An error occurred" >&2

The ./do_something is only executed if the cd command was successful (returned an exit status of zero) and the echo command would be executed only if either the cd or the ./do_something return an error (non zero exit status).

The Linux man page is intended to be the authoritative explanatory document for the understanding of how bash operates, while the GNU manual is sometimes considered more user-friendly for reading.

Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC