Differences between revisions 116 and 166 (spanning 50 versions)
Revision 116 as of 2008-07-07 07:49:27
Size: 471
Editor: Lhunath
Comment: section numbering on.
Revision 166 as of 2013-01-17 20:41:51
Size: 8053
Editor: GreyCat
Comment: remove indents
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## The Bash Guide
## ~ lhunath
#pragma section-numbers on
#acl GreyCat:read,write,revert,admin,delete Lhunath:read,write,revert pgas:read,write,revert All:read
[[BashGuide/CommandsAndArguments|Commands and Arguments ->]]
----
= Introduction =
Line 5: Line 6:
= Table Of Contents = '''You are invited to make additions or modifications so long as you can keep them accurate. Please test any code samples you write.'''
Line 7: Line 8:
[[TableOfContents]]
## Until TableOfContents.py gets fixed, every page will have to be manually included.
## Currently, using the regex makes the ToC have links pointing to the wrong anchors.
## [[Include(^BashGuide/.*, , editlink)]]
All the information here is presented without any warranty or guarantee of accuracy. Use it at your own risk. When in doubt, please consult the man pages or the GNU info pages as the authoritative references.
Line 12: Line 10:
[[Include(BashGuide/Introduction, , editlink)]]
[[Include(BashGuide/TheBasics, , editlink)]]
[[Include(BashGuide/Practices, , editlink)]]
--------
<<Anchor(About)>>
= About This Guide =

This guide aims to aid people interested in learning to work with [[BASH]]. It aspires to teach good practice techniques for using BASH, and writing simple scripts.

This guide is targeted at beginning users. It assumes no advanced knowledge -- just the ability to login to a Unix-like system and open a command-line (terminal) interface. It will help if you know how to use a text editor; we will not be covering editors, nor do we endorse any particular editor choice. Familiarity with the fundamental Unix tool set, or with other programming languages or programming concepts, is not required, but those who have such knowledge may understand some of the examples more quickly.

If something is unclear to you, you are invited to report this (use BashGuideFeedback, or the `#bash` channel on `irc.freenode.org`) so that it may be clarified in this document for future readers.

You are invited to contribute to the development of this document by extending it or correcting invalid or incomplete information.

The primary maintainer(s) of this document:

 * -- [[Lhunath]] (primary author)
 * -- GreyCat

The guide is also available in [[http://stuff.lhunath.com/BashGuide.pdf|PDF format]].
Alternatively, you can just hit print after going to [[FullBashGuide]]. That guarantees you'll be printing the latest version of this document.

--------
<<Anchor(Definition)>>
= A Definition =

BASH is an acronym for '''''B'''ourne '''A'''gain '''Sh'''ell''. It is based on the ''Bourne'' shell and is mostly compatible with its features.

Shells are command interpreters. They are applications that provide users with the ability to give commands to their operating system interactively, or to execute batches of commands quickly. In no way are they required for the execution of programs; they are merely a layer between system function calls and the user.

Think of a shell as a way for you to speak to your system. Your system doesn't need it for most of its work, but it is an excellent interface between you and what your system can offer. It allows you to perform basic math, run basic tests and execute applications. More importantly, it allows you to combine these operations and connect applications to each other to perform complex and automated tasks.

BASH is '''not''' your operating system. It is not your window manager. It is not your terminal (but it oftens runs ''inside'' your terminal). It does not control your mouse or keyboard. It does not configure your system, activate your screensaver, or open your files when you double-click them. It is generally not involved in launching applications from your window manager or desktop environment. It's important to understand that BASH is only an interface for you to execute statements (using BASH syntax), either at the interactive BASH prompt or via BASH scripts.


--------
 . '''In The Manual: [[http://www.gnu.org/software/bash/manual/bashref.html#Introduction|Introduction]]'''
----
 . ''Shell'': A (possibly interactive) command interpreter, acting as a layer between the user and the system. <<BR>> ''Bash'': The Bourne Again Shell, a ''Bourne'' compatible shell.

--------
<<Anchor(Using_Bash)>>
= Using Bash =

Most users that think of BASH think of it as a prompt and a command line. That is BASH in ''interactive mode''. BASH can also run in ''non-interactive mode'', as when executing scripts. We can use scripts to automate certain logic. Scripts are basically lists of commands (just like the ones you can type on the command line), but stored in a file. When a script is executed, all these commands are (generally) executed sequentially, one after another.

We'll start with the basics in an ''interactive shell''. Once you're familiar with those, you can put them together in scripts.

'''Important! <<BR>> You should make yourself familiar with the `man` and `apropos` commands on the shell. They will be vital to your self-tutoring.'''

{{{
$ man man
$ man apropos
}}}

In this guide, the `$` at the beginning of a line represents your BASH prompt. Traditionally, a shell prompt either ends with `$`, `%` or `#`. If it ends with `$`, this indicates a shell that's compatible with the Bourne shell (such as a POSIX shell, or a Korn shell, or Bash). If it ends with `%`, this indicates a ''C shell'' (csh or tcsh); this guide does not cover C shell. If it ends with `#`, this indicates that the shell is running as the system's superuser account (`root`), and that you should be extra careful.

Your actual BASH prompt will probably be much longer than `$`. Prompts are often highly individualized.

The `man` command stands for "manual"; it opens documentation (so-called "man pages") on various topics. You use it by running the command `man [topic]` at the BASH prompt, where `[topic]` is the name of the "page" you wish to read. Note that many of these "pages" are considerably longer than one printed page; nevertheless, the name persists. Each command (application) on your system is likely to have a man page. There are pages for other things too, such as system calls or specific configuration files. In this guide, we will only be covering commands.

Note that if you're looking for information on BASH built-ins (commands provided by BASH, not by external applications) you should look in `man bash` instead. BASH's manual is extensive and detailed. It is an excellent reference, albeit more technical than this guide.

Bash also offers a `help` command which contains brief summaries of its built-in commands (which we'll discuss in depth later on).

{{{
$ help
$ help read
}}}

--------
 . '''In the FAQ: <<BR>> [[BashFAQ/061|Is there a list of which features were added to specific releases (versions) of Bash?]]'''
----
 . ''Interactive mode'': A mode of operation where a prompt asks you for one command at a time.
 . ''Script'': A file that contains a sequence of commands to execute one after the other.
--------

= Contents =

The guide has been divided into sections, which are intended to be read roughly in the order presented. If you skip ahead to a specific section, you might find yourself missing some background information from previous sections. (Links to relevant sections are not always provided when a topic is mentioned.)

 * [[/CommandsAndArguments|Commands and Arguments]]
  * Types of commands; argument splitting; writing scripts.
 * [[/SpecialCharacters|Special Characters]]
 * [[/Parameters|Parameters]]
  * Variables; special parameters; parameter types; parameter expansion.
 * [[/Patterns|Patterns]]
  * Globs; filename matching; extended globs; brace expansion; regular expressions.
 * [[/TestsAndConditionals|Tests and Conditionals]]
  * Exit status; {{{&&}}} and {{{||}}}; if, test and {{{[[}}}; while, until and for; case and select.
 * [[/Arrays|Arrays]]
  * Arrays; associative arrays.
 * [[/InputAndOutput|Input and Output]]
  * Redirection; here documents; here strings; pipes; process substitution.
 * [[/CompoundCommands|Compound Commands]]
  * Subshells; command grouping; arithmetic evaluation; functions; aliases.
 * [[/Sourcing|Sourcing]]
  * Reading commands from other files.
 * [[/JobControl|Job Control]]
 * [[/Practices|Practices]]
  * Choosing your shell; quoting; readability; debugging.

----
[[BashGuide/CommandsAndArguments|Commands and Arguments ->]]
----
CategoryShell

Commands and Arguments ->


Introduction

You are invited to make additions or modifications so long as you can keep them accurate. Please test any code samples you write.

All the information here is presented without any warranty or guarantee of accuracy. Use it at your own risk. When in doubt, please consult the man pages or the GNU info pages as the authoritative references.


About This Guide

This guide aims to aid people interested in learning to work with BASH. It aspires to teach good practice techniques for using BASH, and writing simple scripts.

This guide is targeted at beginning users. It assumes no advanced knowledge -- just the ability to login to a Unix-like system and open a command-line (terminal) interface. It will help if you know how to use a text editor; we will not be covering editors, nor do we endorse any particular editor choice. Familiarity with the fundamental Unix tool set, or with other programming languages or programming concepts, is not required, but those who have such knowledge may understand some of the examples more quickly.

If something is unclear to you, you are invited to report this (use BashGuideFeedback, or the #bash channel on irc.freenode.org) so that it may be clarified in this document for future readers.

You are invited to contribute to the development of this document by extending it or correcting invalid or incomplete information.

The primary maintainer(s) of this document:

The guide is also available in PDF format. Alternatively, you can just hit print after going to FullBashGuide. That guarantees you'll be printing the latest version of this document.


A Definition

BASH is an acronym for Bourne Again Shell. It is based on the Bourne shell and is mostly compatible with its features.

Shells are command interpreters. They are applications that provide users with the ability to give commands to their operating system interactively, or to execute batches of commands quickly. In no way are they required for the execution of programs; they are merely a layer between system function calls and the user.

Think of a shell as a way for you to speak to your system. Your system doesn't need it for most of its work, but it is an excellent interface between you and what your system can offer. It allows you to perform basic math, run basic tests and execute applications. More importantly, it allows you to combine these operations and connect applications to each other to perform complex and automated tasks.

BASH is not your operating system. It is not your window manager. It is not your terminal (but it oftens runs inside your terminal). It does not control your mouse or keyboard. It does not configure your system, activate your screensaver, or open your files when you double-click them. It is generally not involved in launching applications from your window manager or desktop environment. It's important to understand that BASH is only an interface for you to execute statements (using BASH syntax), either at the interactive BASH prompt or via BASH scripts.



  • Shell: A (possibly interactive) command interpreter, acting as a layer between the user and the system.
    Bash: The Bourne Again Shell, a Bourne compatible shell.


Using Bash

Most users that think of BASH think of it as a prompt and a command line. That is BASH in interactive mode. BASH can also run in non-interactive mode, as when executing scripts. We can use scripts to automate certain logic. Scripts are basically lists of commands (just like the ones you can type on the command line), but stored in a file. When a script is executed, all these commands are (generally) executed sequentially, one after another.

We'll start with the basics in an interactive shell. Once you're familiar with those, you can put them together in scripts.

Important!
You should make yourself familiar with the man and apropos commands on the shell. They will be vital to your self-tutoring.

$ man man
$ man apropos

In this guide, the $ at the beginning of a line represents your BASH prompt. Traditionally, a shell prompt either ends with $, % or #. If it ends with $, this indicates a shell that's compatible with the Bourne shell (such as a POSIX shell, or a Korn shell, or Bash). If it ends with %, this indicates a C shell (csh or tcsh); this guide does not cover C shell. If it ends with #, this indicates that the shell is running as the system's superuser account (root), and that you should be extra careful.

Your actual BASH prompt will probably be much longer than $. Prompts are often highly individualized.

The man command stands for "manual"; it opens documentation (so-called "man pages") on various topics. You use it by running the command man [topic] at the BASH prompt, where [topic] is the name of the "page" you wish to read. Note that many of these "pages" are considerably longer than one printed page; nevertheless, the name persists. Each command (application) on your system is likely to have a man page. There are pages for other things too, such as system calls or specific configuration files. In this guide, we will only be covering commands.

Note that if you're looking for information on BASH built-ins (commands provided by BASH, not by external applications) you should look in man bash instead. BASH's manual is extensive and detailed. It is an excellent reference, albeit more technical than this guide.

Bash also offers a help command which contains brief summaries of its built-in commands (which we'll discuss in depth later on).

$ help
$ help read



  • Interactive mode: A mode of operation where a prompt asks you for one command at a time.

  • Script: A file that contains a sequence of commands to execute one after the other.


Contents

The guide has been divided into sections, which are intended to be read roughly in the order presented. If you skip ahead to a specific section, you might find yourself missing some background information from previous sections. (Links to relevant sections are not always provided when a topic is mentioned.)


Commands and Arguments ->


CategoryShell

BashGuide (last edited 2021-05-27 20:29:49 by GreyCat)