Differences between revisions 30 and 35 (spanning 5 versions)
Revision 30 as of 2014-11-28 07:10:18
Size: 7
Editor: 148
Comment:
Revision 35 as of 2015-01-20 20:24:29
Size: 5058
Editor: dynamic-adsl-94-39-245-186
Comment: some typos
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
sdasd ## page was renamed from BashGuide/02.SpecialCharacters
[[BashGuide/CommandsAndArguments|<- Commands and Arguments]] | [[BashGuide/Parameters|Parameters ->]]

----

<<Anchor(StartOfContent)>>

= Special characters =

A group of characters have been exempted, that when we use them, they 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".

To explain the special characters in all the cases in which they may be used, read the section on the [[http://www.tldp.org/LDP/abs/html/special-chars.html|TLDP guide]]. 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'' — an introduction of a `#` character begins a commentary that extends to the end of the line. Comments are notes of explanation and are not processed by the shell. ||
|| `[[]]` || ''Test'' — an evaluation of a conditional expression to determine whether it is "true" or "false". Tests are used in Bash to evaluate a number of conditions. 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''. Redirections will be covered later. ||
|| `|` || ''Pipe'' — redirect output from a initial command to the input of secondary command. This is a method of chaining commands together. Example: `echo "Hello beautiful." | grep -o beautiful`. ||
|| `;` || ''Command separator'' — a representation of a newline. 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 subshell. 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 ))"`. ||
|| `~` || ''Home directory'' — the tilde is a representation of the home directory. When followed by a `/`, it means the current user's home directory; otherwise, a username will have to be specified (e.g. `ls ~/Documents; cp ~john/.bashrc .`). ||

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.
}}}

'''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

A group of characters have been exempted, that when we use them, they 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".

To explain the special characters in all the cases in which they may be used, read the section on the TLDP guide. 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 — an introduction of a # character begins a commentary that extends to the end of the line. Comments are notes of explanation and are not processed by the shell.

[[]]

Test — an evaluation of a conditional expression to determine whether it is "true" or "false". Tests are used in Bash to evaluate a number of conditions. 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. Redirections will be covered later.

|

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

;

Command separator — a representation of a newline. 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 subshell. 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 ))".

~

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

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.

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)