Differences between revisions 1 and 28 (spanning 27 versions)
Revision 1 as of 2008-05-14 13:51:42
Size: 3512
Editor: Lhunath
Comment:
Revision 28 as of 2014-03-26 19:00:13
Size: 4906
Editor: bwe
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from BashGuide/02.SpecialCharacters
[[BashGuide/CommandsAndArguments|<- Commands and Arguments]] | [[BashGuide/Parameters|Parameters ->]]
----
<<Anchor(StartOfContent)>>
= Special Characters =
Line 2: Line 7:
[[Anchor(Special_Characters)]]
== Special Characters ==

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.
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.  These are also called ''metacharacters''.
Line 9: Line 11:
 * '''''[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.
 * '''[[ 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.
 * '''''[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.
 * '''{{{$}}}''': Expansion character. This character is used in most substitutions, including parameter expansion (variable substitution). More about this later.
 * '''{{{'text'}}}''': Single quotes protect the text inside from any kind of expansion by the shell and keep it from being split into multiple words or arguments. They also prevent the special meaning of all special characters inside.
 * '''{{{"text"}}}''': Double quotes protect the text inside from being split into multiple words or arguments, but they permit substitutions to occur. They prevent the special meaning of ''most'' special characters inside them -- basically, all except for the `$` and some others.
 * '''{{{#}}}''': 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 prevents the next character from being used in any special sort of way. This works inside double quotes, or outside of all quotes, but ''not'' inside single quotes.
 * '''{{{~}}}''': The tilde is a shortcut for your home directory. By itself, or when followed by a `/`, it is the same as `$HOME`. When followed by a username, it means ''that user's'' home directory. Examples: `cd ~john/bin; cp coolscript ~/bin`
 * '''{{{>}}}''' or '''{{{<}}}''': Redirection characters. These characters are used to modify (redirect) the input and/or output of a command. Redirections will be covered later.
 * '''{{{|}}}''': Pipelines allow you to send the output of one command as the input to another command.
 * '''{{{[[ expression ]]}}}''': Test expression. This evaluates the conditional expression as a logical statement, to determine whether it's "true" or "false".
 * '''{{{ { commands; } }}}''': Command grouping. The commands inside the braces are treated as though they were only one command. It is convenient for places where Bash syntax requires only one command to be present, and you don't feel a function is warranted.
 * '''{{{`command`}}}''', '''{{{$(command)}}}''': [[CommandSubstitution|Command substitution]] (The latter form is '''highly''' preferred.) Command substitution executes the inner command first, and then replaces the whole {{{`...`}}} or {{{$(...)}}} with that command's standard output.
 * '''{{{(command)}}}''': [[SubShell|Subshell]] Execution. This executes the command in a new bash shell, instead of in the current one, like a safe sandbox. If the command causes side effects (like changing variables), those changes will have no effect on the current shell.
 * '''{{{((expression))}}}''': [[ArithmeticExpression|Arithmetic]] Command. Inside the parentheses, operators such as +, -, * and / are seen as mathematical operators. This can be used for assignments like `((a=b+7))` as well as tests like `if ((a < b))`. More on this later.
 * '''{{{$((expression))}}}''': Arithmetic Substitution. Comparable to the above, but this expression is replaced with the result of its arithmetic evaluation. Example: `echo "The average is $(( (a+b)/2 ))"`.
Line 24: Line 29:
Line 26: Line 30:
    $ 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 bigger than zero."
Five is bigger than zero.
Line 42: Line 46:
 . '''In The Manual: [http://www.gnu.org/software/bash/manual/bashref.html#SEC6 Shell Syntax]'''  . '''In The Manual: [[http://www.gnu.org/software/bash/manual/html_node/Shell-Syntax.html#Shell-Syntax|Shell Syntax]]'''
Line 44: Line 48:
 . ''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.  . ''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)>>
Line 46: Line 51:
[[BashGuide/CommandsAndArguments|<- Commands and Arguments]] | [[BashGuide/Parameters|Parameters ->]]

<- Commands and Arguments | Parameters ->


Special Characters

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. These are also called metacharacters.

Here are a few of those special characters, and what they do:

  • [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.

  • $: Expansion character. This character is used in most substitutions, including parameter expansion (variable substitution). More about this later.

  • 'text': Single quotes protect the text inside from any kind of expansion by the shell and keep it from being split into multiple words or arguments. They also prevent the special meaning of all special characters inside.

  • "text": Double quotes protect the text inside from being split into multiple words or arguments, but they permit substitutions to occur. They prevent the special meaning of most special characters inside them -- basically, all except for the $ and some others.

  • #: 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 prevents the next character from being used in any special sort of way. This works inside double quotes, or outside of all quotes, but not inside single quotes.

  • ~: The tilde is a shortcut for your home directory. By itself, or when followed by a /, it is the same as $HOME. When followed by a username, it means that user's home directory. Examples: cd ~john/bin; cp coolscript ~/bin

  • > or <: Redirection characters. These characters are used to modify (redirect) the input and/or output of a command. Redirections will be covered later.

  • |: Pipelines allow you to send the output of one command as the input to another command.

  • [[ expression ]]: Test expression. This evaluates the conditional expression as a logical statement, to determine whether it's "true" or "false".

  •  { commands; } : Command grouping. The commands inside the braces are treated as though they were only one command. It is convenient for places where Bash syntax requires only one command to be present, and you don't feel a function is warranted.

  • `command`, $(command): Command substitution (The latter form is highly preferred.) Command substitution executes the inner command first, and then replaces the whole `...` or $(...) with that command's standard output.

  • (command): Subshell Execution. This executes the command in a new bash shell, instead of in the current one, like a safe sandbox. If the command causes side effects (like changing variables), those changes will have no effect on the current shell.

  • ((expression)): Arithmetic Command. Inside the parentheses, operators such as +, -, * and / are seen as mathematical operators. This can be used for assignments like ((a=b+7)) as well as tests like if ((a < b)). More on this later.

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

Some 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 bigger than zero."
Five is bigger than zero.



  • 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)