Encoder sur Linux
# Encoder un string (sans saut de ligne)
echo -n 'Hello World' | base64
# SGVsbG8gV29ybGQ=
# Encoder un fichier sans retours à la ligne
base64 -w 0 image.png > image.b64Décoder sur Linux
# Décoder un string
echo 'SGVsbG8gV29ybGQ=' | base64 -d
# Décoder un fichier
base64 -d image.b64 > image.pngLinux vs macOS
Linux : -d pour décoder, -w 0 sans retours. macOS : -D et -b 0 respectivement.
FAQ
Pourquoi utiliser echo -n ?
Sans -n, echo ajoute un saut de ligne qui est inclus dans l'encodage, produisant un résultat incorrect.