Bash: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
Bash is the program that is the command line itself. BASH stands for the "Bourne-Again" Shell, the default shell for Linux and Mac OS X (Bourne-again in reference an original UNIX shell written by Stephen Bourne; BASH is a free software rewrite, part of the GNU project). | Bash is the program that is the command line itself. BASH stands for the "Bourne-Again" Shell, the default shell for Linux and Mac OS X (Bourne-again in reference an original UNIX shell written by Stephen Bourne; BASH is a free software rewrite, part of the GNU project). | ||
BASH scripts are, in the simplest form, just a sequence of command line commands saved in a file (or files). In this way, you can save a complicated series of steps in a single file (or BASH script). Using '''variables''', you can turn a script into a tool that can, for instance, be applied to different input files, or perform an image transformation to a variable degree. | BASH scripts are, in the simplest form, just a sequence of command line commands saved in a file (or files). In this way, you can save a complicated series of steps in a single file (or BASH script). Using '''variables''', you can turn a script into a tool that can, for instance, be applied to different input files, or perform an image transformation to a variable degree. | ||
== Variables == | |||
<pre> | |||
name="Francesco" | |||
echo Hello there $name | |||
</pre> | |||
NB: Use no variables when setting a value (name=). Unlike most scripting languages, BASH isn't so forgiving of extra spaces and gets confused if you add them. Notice also how unlike most programming languages, in BASH text is text first, and interpreted as an expression by exception. | |||
The simplest way to recall a variable (have it's value substituted at the place where you refer to it) is with the $ character. The general form is to use curly braces, e.g.: | |||
<pre> | |||
echo Hello there ${name} | |||
echo Hello${name}Hello! | |||
</pre> | |||
This can be useful when the surrounding characters would otherwise make it unclear when the variable name ends (as in the second case above). | |||
Special forms: | |||
{| class="wikitable" | |||
| ${name:-Joe} || ''Return a default value if the variable is undefined'' <br /> If name exists and isn't null, return its value; otherwise, return 'Joe'. || | |||
|- | |||
| ${name:=Joe} || ''Sets a default value if the variable is undefined'' <br /> Return the value of name if it exists and isn't null; otherwise, set it to 'Joe' and return that. || | |||
|- | |||
| ${name:+value} || ''Test for a variable being set'' <br /> Return the value if name is defined || | |||
|- | |||
| ${name:?message} || ''Catches errors from a variable being undefined'' <br /> Return the value of name if it exists and isn't null; otherwise, prints the message and stops the script (though not guaranteed to stop -- depends on shell?) || | |||
|- | |||
|} | |||
"Pattern-strippers" (with [[ShellWildcards]], note that these are different from a *[[Regular Expression]]*) | |||
{| class="wikitable" | |||
| ${variable#pattern} || Delete pattern \from start (smallest match) || | |||
|- | |||
| ${variable##pattern} || Delete pattern \from start (longest match) || | |||
|- | |||
| ${variable%pattern} || Delete pattern \from end (smallest match) || | |||
|- | |||
| ${variable%%pattern} || Delete pattern \from end (longest match) || | |||
|- | |||
== Fundamental Concepts == | == Fundamental Concepts == |
Revision as of 23:01, 11 October 2010
Bash is the program that is the command line itself. BASH stands for the "Bourne-Again" Shell, the default shell for Linux and Mac OS X (Bourne-again in reference an original UNIX shell written by Stephen Bourne; BASH is a free software rewrite, part of the GNU project).
BASH scripts are, in the simplest form, just a sequence of command line commands saved in a file (or files). In this way, you can save a complicated series of steps in a single file (or BASH script). Using variables, you can turn a script into a tool that can, for instance, be applied to different input files, or perform an image transformation to a variable degree.
Variables
name="Francesco" echo Hello there $name
NB: Use no variables when setting a value (name=). Unlike most scripting languages, BASH isn't so forgiving of extra spaces and gets confused if you add them. Notice also how unlike most programming languages, in BASH text is text first, and interpreted as an expression by exception.
The simplest way to recall a variable (have it's value substituted at the place where you refer to it) is with the $ character. The general form is to use curly braces, e.g.:
echo Hello there ${name} echo Hello${name}Hello!
This can be useful when the surrounding characters would otherwise make it unclear when the variable name ends (as in the second case above).
Special forms:
${name:-Joe} | Return a default value if the variable is undefined If name exists and isn't null, return its value; otherwise, return 'Joe'. |
|
${name:=Joe} | Sets a default value if the variable is undefined Return the value of name if it exists and isn't null; otherwise, set it to 'Joe' and return that. |
|
${name:+value} | Test for a variable being set Return the value if name is defined |
|
${name:?message} | Catches errors from a variable being undefined Return the value of name if it exists and isn't null; otherwise, prints the message and stops the script (though not guaranteed to stop -- depends on shell?) |
"Pattern-strippers" (with ShellWildcards, note that these are different from a *Regular Expression*)
Fundamental Concepts
The BASH has most of the standard features of a programming language, including:
- Variable Substitution
- Arithmetic Expansion
- Command Substitution
- For Loops
- While Loops
- If-Then (Else) Clauses
- Functions
Key Terms
- Shell:: The program that is running while you use the command line (or the Terminal on a Mac)
- BASH:: The "Bourne-Again" Shell
- Environment Variables:: The variables (named pieces of text information) that the shell uses to keep track of important system settings. It was the Bourne shell that first added a notion of variables, a crucial step in making the shell a programming environment.
- echo:: The BASH command to display something (the "print" statement of the shell)
- $PATH:: The environment variable that tells the system where to find programs. When you type a command, the shell will search the places listed in the PATH. (The path contents are a list of UNIX directories separated by colons (:))
- Dot-files:: Files that start with a period (.). They are usually hidden (but you can see them with the ls -a command). Usually dot-files are used to store special settings for an application. The shell uses several (like the .profile where you can put shell commands that happen when you start your shell -- though this may depend on which shell program you use).
Resources
Other tutorials for BASH scripting:
- http://www.gentoo.org/doc/en/articles/bash-by-example-p1.xml
- http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
- http://tldp.org/LDP/abs/html/
Related pages
MediaBashing MoreMediaBashing grep
${variable#pattern} | Delete pattern \from start (smallest match) | |
${variable##pattern} | Delete pattern \from start (longest match) | |
${variable%pattern} | Delete pattern \from end (smallest match) | |
${variable%%pattern} | Delete pattern \from end (longest match) |