Differences between revisions 11 and 39 (spanning 28 versions)
Revision 11 as of 2009-02-13 23:46:30
Size: 3709
Editor: GreyCat
Comment: markup fixes, and add the pipe
Revision 39 as of 2019-02-14 20:04:40
Size: 5943
Editor: GreyCat
Comment: Add =, &, globs, and (deprecated) $[ ], remove TLDP link, clean up a bit.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<Anchor(Special_Characters)>>
== Special Characters ==
## page was renamed from BashGuide/02.SpecialCharacters
[[BashGuide/CommandsAndArguments|<- Commands and Arguments]] | [[BashGuide/Parameters|Parameters ->]]
Line 4: Line 4:
There are several special characters in [[BASH]] that have a non-literal meaning. When we use these characters, [[BASH]] evaluates these characters and their meaning, but usually does not pass them on to the underlying commands. ----
Line 6: Line 6:
Here are a few of those special characters, and what they do: <<Anchor(StartOfContent)>>
Line 8: Line 8:
 * '''''[whitespace]''''': Whitespace (spaces, tabs and newlines). [[BASH]] uses whitespace to determine where words begin and end. The first word of each command is used as the command name; any additional words become arguments to that command.
 * '''{{{"text"}}}''': Double quotes. Double quotes protect the text inside from being split into multiple words or arguments. They also prevent the special meaning of single quotes inside. However, other special characters retain their special meanings.
 * '''{{{'text'}}}''': Single quotes. Single quotes protect the text inside from any kind of expansion by the shell and keeps it from being split into multiple words or arguments. They also prevent the special meaning of all special characters inside.
 * '''{{{# text}}}''': Comment character. Any word beginning with `#` begins a ''comment'' that extends to the next newline. Comments are not processed by the shell.
 * '''{{{;}}}''': Command separator. The semicolon is used to separate multiple commands from each other if the user chooses to keep them on the same line. It's ''basically'' the same thing as a newline.
 * '''{{{\}}}''': Escape character. The backslash protects the next character from being used in any special sort of way.
 * '''{{{>}}}''' or '''{{{<}}}''': Redirection character. These characters are used to modify (redirect) the input and/or output of a command.
 * '''{{{|}}}''': Pipeline. Pipelines allow you to send the output of one command as the input to another command.
 * '''{{{[[ expression ]]}}}''': Test expression. This evaluates the conditional expression.
 * '''{{{ { commands; } }}}''': Command Group. This executes the commands inside the braces as though they were only one command. It is convenient for places where [[BASH]] syntax requires only one command to be present.
 * '''{{{`command`}}}''', '''{{{$(command)}}}''': Command substitution (The latter form is '''highly''' preferred). Command substitution executes the command inside the substitution form first, and replaces itself by that command's output.
 * '''{{{(command)}}}''': Subshell Execution. This executes the command in a new bash shell, instead of in the current.
 * '''{{{((expression))}}}''': Arithmetic Evaluation. Inside the parentheses, operators such as +, -, * and / are seen as mathematical operators.
 * '''{{{$((expression))}}}''': Arithmetic Expansion. Comparable to the above, but this expression is replaced with the result of its arithmetic evaluation.
 * '''{{{$}}}''': Expansion character. This character is used for any form of parameter expansion. More about this later.
Some examples:
= Special characters =

Some characters are evaluated by [[Bash]] to have a ''non-literal'' meaning. Instead, these characters carry out a special instruction, or have an alternate meaning; they are called "special characters", or "meta-characters".

Here are some of the more common special characters uses:

|| '''Char.''' || '''Description''' ||
|| `" "` || ''Whitespace'' — this is a tab, newline, vertical tab, form feed, carriage return, or space. Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command. ||
|| `$` || ''Expansion'' — introduces various types of expansion: parameter expansion (e.g. `$var` or `${var}`), [[CommandSubstitution|command substitution]] (e.g. `$(command)`), or arithmetic expansion (e.g. `$((expression))`). More on expansions later. ||
|| `''` || ''Single quotes'' — protect the text inside them so that it has a ''literal'' meaning. With them, generally any kind of interpretation by Bash is ignored: special characters are passed over and multiple words are prevented from being split. ||
|| `""` || ''Double quotes'' — protect the text inside them from being split into multiple words or arguments, yet allow substitutions to occur; the meaning of most other special characters is usually prevented. ||
|| `\` || ''Escape'' — (backslash) prevents the next character from being interpreted as a special character. This works outside of quoting, inside double quotes, and generally ignored in single quotes. ||
|| `#` || ''Comment'' — the `#` character begins a commentary that extends to the end of the line. Comments are notes of explanation and are not processed by the shell. ||
|| `=` || ''Assignment'' -- assign a value to a variable (e.g. `logdir=/var/log/myprog`). Whitespace is ''not'' allowed on either side of the `=` character. ||
|| `[[ ]]` || ''Test'' — an evaluation of a conditional expression to determine whether it is "true" or "false". Tests are used in Bash to compare strings, check the existence of a file, etc. More of this will be covered later. ||
|| `!` || ''Negate'' — used to negate or reverse a test or exit status. For example: `! grep text file; exit $?`. ||
|| `>`, `>>`, `<` || ''Redirection'' — redirect a command's ''output'' or ''input'' to a file. Redirections will be covered later. ||
|| `|` || ''Pipe'' — send the output from one command to the input of another command. This is a method of chaining commands together. Example: `echo "Hello beautiful." | grep -o beautiful`. ||
|| `;` || ''Command separator'' — used to separate multiple commands that are on the same line. ||
|| `{ }` || ''Inline group'' — commands inside the curly braces are treated as if they were one command. It is convenient to use these when Bash syntax requires only one command and a function doesn't feel warranted. ||
|| `( )` || ''Subshell group'' — similar to the above but where commands within are executed in a [[SubShell|subshell]] (a new process). Used much like a sandbox, if a command causes side effects (like changing variables), it will have no effect on the current shell. ||
|| `(( ))` || ''Arithmetic expression'' — with an [[ArithmeticExpression|arithmetic expression]], characters such as `+`, `-`, `*`, and `/` are mathematical operators used for calculations. They can be used for variable assignments like `(( a = 1 + 4 ))` as well as tests like `if (( a < b ))`. More on this later. ||
|| `$(( ))` || ''Arithmetic expansion'' — Comparable to the above, but the expression is replaced with the result of its arithmetic evaluation. Example: `echo "The average is $(( (a+b)/2 ))"`. ||
|| `*`, `?` || ''[[glob|Globs]]'' -- "wildcard" characters which match parts of filenames (e.g. `ls *.txt`). ||
|| `~` || ''Home directory'' — the tilde is a representation of a home directory. When alone or followed by a `/`, it means the current user's home directory; otherwise, a username must be specified (e.g. `ls ~/Documents; cp ~john/.bashrc .`). ||
|| `&` || ''Background'' -- when used at the end of a command, run the command in the background (do not wait for it to complete). ||

Examples:
Line 26: Line 38:
    $ echo "I am $LOGNAME"
    I am lhunath
    $ echo 'I am $LOGNAME'
    I am $LOGNAME
    $ # boo
    $ echo An open\ \ \ space
    An open space
    
$ echo "My computer is $(hostname)"
    My computer is Lyndir
    $ echo boo > file
    $ echo $(( 5 + 5 ))
    10
    $ (( 5 > 0 )) && echo "Five is bigger than zero."
    Five is bigger than zero.
$ echo "I am $LOGNAME"
I am lhunath
$ echo 'I am $LOGNAME'
I am $LOGNAME
$ # boo
$ echo An open\ \ \ space
An open space
$ echo "My computer is $(hostname)"
My computer is Lyndir
$ echo boo > file
$ echo $(( 5 + 5 ))
10
$ (( 5 > 0 )) && echo "Five is greater than zero."
Five is
greater than zero.
Line 41: Line 53:
--------
 . '''In The Manual: [[http://www.gnu.org/software/bash/manual/bashref.html#SEC6|Shell Syntax]]'''
----
 . ''Special Characters'': Characters that have a special meaning to [[BASH]]. Usually their meaning is interpreted and afterwards they are removed from the command before executing it.
--------

= Deprecated special characters (recognized, but not recommended) =

This group of characters will also be evaluated by [[Bash]] to have a ''non-literal'' meaning, but are generally included for backwards compatibility only. These are not recommended for use, but often appear in older or poorly written scripts.

|| '''Char.''' || '''Description''' ||
|| {{{` `}}} || ''[[CommandSubstitution|Command substitution]]'' - use `$( )` instead. ||
|| `[ ]` || ''Test'' - an alias for the old `test` command. Commonly used in POSIX shell scripts. Lacks many features of `[[ ]]`. ||
|| `$[ ]` || ''[[ArithmeticExpression|Arithmetic expression]]'' - use `$(( ))` instead. ||


'''Additionally:'''

 * '''In The Manual — [[http://www.gnu.org/software/bash/manual/html_node/Shell-Syntax.html#Shell-Syntax|Shell Syntax]]'''
 * ''Special Characters'' — Characters that have a special meaning to Bash. Usually their meaning is interpreted and then they are removed from the command before executing it.

<<Anchor(EndOfContent)>>

[[BashGuide/CommandsAndArguments|<- Commands and Arguments]] | [[BashGuide/Parameters|Parameters ->]]

<- Commands and Arguments | Parameters ->


Special characters

Some characters are evaluated by Bash to have a non-literal meaning. Instead, these characters carry out a special instruction, or have an alternate meaning; they are called "special characters", or "meta-characters".

Here are some of the more common special characters uses:

Char.

Description

" "

Whitespace — this is a tab, newline, vertical tab, form feed, carriage return, or space. Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command.

$

Expansion — introduces various types of expansion: parameter expansion (e.g. $var or ${var}), command substitution (e.g. $(command)), or arithmetic expansion (e.g. $((expression))). More on expansions later.

''

Single quotes — protect the text inside them so that it has a literal meaning. With them, generally any kind of interpretation by Bash is ignored: special characters are passed over and multiple words are prevented from being split.

""

Double quotes — protect the text inside them from being split into multiple words or arguments, yet allow substitutions to occur; the meaning of most other special characters is usually prevented.

\

Escape — (backslash) prevents the next character from being interpreted as a special character. This works outside of quoting, inside double quotes, and generally ignored in single quotes.

#

Comment — the # character begins a commentary that extends to the end of the line. Comments are notes of explanation and are not processed by the shell.

=

Assignment -- assign a value to a variable (e.g. logdir=/var/log/myprog). Whitespace is not allowed on either side of the = character.

[[ ]]

Test — an evaluation of a conditional expression to determine whether it is "true" or "false". Tests are used in Bash to compare strings, check the existence of a file, etc. More of this will be covered later.

!

Negate — used to negate or reverse a test or exit status. For example: ! grep text file; exit $?.

>, >>, <

Redirection — redirect a command's output or input to a file. Redirections will be covered later.

|

Pipe — send the output from one command to the input of another command. This is a method of chaining commands together. Example: echo "Hello beautiful." | grep -o beautiful.

;

Command separator — used to separate multiple commands that are on the same line.

{ }

Inline group — commands inside the curly braces are treated as if they were one command. It is convenient to use these when Bash syntax requires only one command and a function doesn't feel warranted.

( )

Subshell group — similar to the above but where commands within are executed in a subshell (a new process). Used much like a sandbox, if a command causes side effects (like changing variables), it will have no effect on the current shell.

(( ))

Arithmetic expression — with an arithmetic expression, characters such as +, -, *, and / are mathematical operators used for calculations. They can be used for variable assignments like (( a = 1 + 4 )) as well as tests like if (( a < b )). More on this later.

$(( ))

Arithmetic expansion — Comparable to the above, but the expression is replaced with the result of its arithmetic evaluation. Example: echo "The average is $(( (a+b)/2 ))".

*, ?

Globs -- "wildcard" characters which match parts of filenames (e.g. ls *.txt).

~

Home directory — the tilde is a representation of a home directory. When alone or followed by a /, it means the current user's home directory; otherwise, a username must be specified (e.g. ls ~/Documents; cp ~john/.bashrc .).

&

Background -- when used at the end of a command, run the command in the background (do not wait for it to complete).

Examples:

$ echo "I am $LOGNAME"
I am lhunath
$ echo 'I am $LOGNAME'
I am $LOGNAME
$ # boo
$ echo An open\ \ \ space
An open   space
$ echo "My computer is $(hostname)"
My computer is Lyndir
$ echo boo > file
$ echo $(( 5 + 5 ))
10
$ (( 5 > 0 )) && echo "Five is greater than zero."
Five is greater than zero.

Deprecated special characters (recognized, but not recommended)

This group of characters will also be evaluated by Bash to have a non-literal meaning, but are generally included for backwards compatibility only. These are not recommended for use, but often appear in older or poorly written scripts.

Char.

Description

` `

Command substitution - use $( ) instead.

[ ]

Test - an alias for the old test command. Commonly used in POSIX shell scripts. Lacks many features of [[ ]].

$[ ]

Arithmetic expression - use $(( )) instead.

Additionally:

  • In The Manual — Shell Syntax

  • Special Characters — Characters that have a special meaning to Bash. Usually their meaning is interpreted and then they are removed from the command before executing it.

<- Commands and Arguments | Parameters ->

BashGuide/SpecialCharacters (last edited 2019-02-14 20:04:40 by GreyCat)