# printf



| Character | Usage                                                    |
| --------- | -------------------------------------------------------- |
| %s        | String                                                   |
| %c        | Single character                                         |
| %d        | Integers                                                 |
| %o        | Octal integers                                           |
| %u        | unsigned integer                                         |
| %x        | Hexadecimal integers (minuscule)                         |
| %X        | Hexadecimal integers (majuscule)                         |
| %f        | Floating point                                           |
| %e        | floating point number in scientific notation (minuscule) |
| %E        | floating point number in scientific notation (majuscule) |
| %b        | String with backslash escape character                   |
| %%        | Percent sign                                             |



```bash
printf "\t %3s %-40s \n" "${index}." "${track}"

# tab
# chaine de 3 caractères alignés à droite
# chaine de 40 caractères alignés à gauche
```

```
	  9.  Party!
	 10.  Shooters
```



#### Modificateur de précision:

. (point) suivi d'un nombre spécifie le nombre de chiffres à afficher. 

```bash
$ printf "Number: %.5d\n" 23

Number: 00023
```

avec une chaine:

```bash
printf "Nom: %.7s\n" Terminator
Nom: Termina
```



#### Afficher une ligne de tiret:

```bash
printf "%80s\n" " " | tr ' ' '-' 

------------------------------------------------------------------------------------------------

```



#### Afficher une chaine avec des accents:

```bash
title="France Inter 95.9 (Émissions-débats France)"

# https://unix.stackexchange.com/questions/609125/padding-unicode-strings-with-bashs-printf
bytes=$(printf '%s' "$title" | wc -c)
chars=$(printf '%s' "$title" | wc -m)
n=$((50+bytes-chars))

printf '%-${n}s' "${title}"
```



#### Afficher une chaine en couleur

```bash
red="\033[1;31m"
reset="\033[0m"

printf " %-11s  %-35b \n" "Width:" "${red}${width}${reset}"

# %b     ARGUMENT as a string with '\' escapes interpreted, except that octal
#      escapes are of the form \0 or \0NNN
```



Séparateur de millier ( ' ) 

```bash
LC_ALL=en_US.UTF-8 printf "| %-15s | %-4s | %'9.2f | %'9.2f | %'14d \n" "$name" "$symbol" "$price" "$price2" "$volume_24h"

| Bitcoin         | BTC  | 94,289.45 | 97,788.72 | 50,548,740,838 |


LC_ALL=fr_FR.UTF-8 printf "| %-15s | %-4s | %'9.2f | %'9.2f | %'14d \n" "$name" "$symbol" "$price" "$price2" "$volume_24h"

| Bitcoin         | BTC  |  94405,00 |  97909,00 |    50580369313 |

printf "| %-15s | %-4s | %'9.2f | %'9.2f | %'14d \n" "$name" "$symbol" "$price" "$price2" "$volume_24h"

| Bitcoin         | BTC  |  94348.51 |  97837.60 |    50613422301 |
```

https://stackoverflow.com/questions/9374868/number-formatting-in-bash-with-thousand-separator
