Skip to content
Snippets Groups Projects
bash-5-special_characters.md 3.63 KiB
Newer Older
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
# Tips

## Special Character

There are plenty of special characters in bash:

| Character | Meaning | type 
| :-: | :-: | :-: 
| ~ |	Home directory	|
| ` |	Command substitution (archaic)	|
| # |	Comment	|
| $	| Variable expression |
| &	| Background job |
| (	| Start subshell |
| )	| End subshell |
| \	| Quote next character	|
| \| | Pipe	|
| {	| Start command block	|
| }	| End command block	|
| ;	| Shell command separator	|
| '	| Strong quote	|
| "	| Weak quote	|
| <	| Input redirect	|
| >	| Output redirect	|
| /	| Pathname directory separator	|
| !	| history expansion |
| ?	| Single-character wildcard	| globbing
| *	| String wildcard | globbing 
| [	| Start character-set wildcard	| globbing
| ]	| End character-set wildcard	| globbing

So far we have seen `$`, `!`, `;` , `~`  
We will learn few more within this course, but not need to learn all of them.

## Quotes

We have seen the `echo` command to print text.
Try this command and check what happens.

```bash
echo (
```

You will get an error message because there is a special character `(` in your command. Special characters are useful but must be used in a specific way to be interpreted by bash as intended.  
But then how should I do to use it as a normal character and not a special character?  Let's talk about the quotes...

### Single quotes

Wa have how to create a variable `var=4` and how to display the variable's value `echo $var`.

Give a try printing a variable **with** and **without** the single quote `'` e.g.

```bash
var=4
echo $var
echo '$var'
```

!!! question "What happens with the single quotes?"

??? example "Click to show the solution"
    Wihtout the simple quote we can display the variable's value (data interpolation).
    With the simple quote it display literraly `$var`.  

It works with any special character... let's have a second try with our first example:
```bash
echo '('
```

The main point of using single quotes in Bash `'` is to preserve the literal value of each character within the quotes.
Single quotes strip any special value and avoid any data interpolation.

### Double quotes

Then what about double quotes? Actually double quotes `"` preserves the literal value of all characters **except** for `$`, `\``, `"`, `\`, and `!` special characters.

Let's have a try with the previous examples:
```bash
echo "$var"
echo "("
```

We can observe in the variable case, it's value is printed, while it was not the case for the single quote.  
In the second example the litteral value `(` is printed. 

### Escaping
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
![](../images/tips/Unknown.jpeg){: style="height:100px" align=right}  
Not from the room...  
In bash it exists a *special character allowing to escape a special character*.  
You can avoid a special character to be interpreted as "special" by adding a backslash `\`.  
 
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
Let's give a try to make it clearer:

```bash
echo $var
echo \$var
echo "$var"
echo "\$var"
echo (
echo \(
```

Find a way print the following sentence `my value $var is 4 (interesting)` with an `echo` command while the value 4 must come from the variable $var.

??? example "Click to show the solution"
    `echo "my value \$var is $var (interesting)"`  
    or
    `echo my value \$var is $var \(interesting\)`

Bash is extraordinary powerfull, but stupid as any computer... it just applies rules. There are many characters with specific meanings and combined together it increase the complexity. That may become difficult for the user to read or understand. But bash it its side will just follow the rules!

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
!!! Success "Quick recap"
    In this section we've learned:

    - the use of single and double quotes and their behavior towards special characters.
    - How to espace special characters with `\`