b64 encode bash
- User
b64 encode bash
- Assistant
In Bash, you can use the
base64command to encode and decode data in Base64 format. Here's how you can do it:Encode a String
To encode a string in Base64, you can use the
echocommand combined withbase64. For example:bashecho -n "Hello, World!" | base64The
-nflag forechois used to prevent it from appending a newline character at the end of the string, which would also get encoded.Encode a File
If you want to encode the contents of a file, you can use:
bashbase64 filename.txtReplace
filename.txtwith the name of your file. This command will output the Base64-encoded content to the terminal.Decode a Base64 String
To decode a Base64-encoded string, use the
-dflag withbase64:bashecho "SGVsbG8sIFdvcmxkIQ==" | base64 -dDecode a Base64 Encoded File
To decode a Base64-encoded file back to its original form, you can do:
bashbase64 -d encodedfile.txt > decodedfile.txtReplace
encodedfile.txtwith your Base64-encoded file name anddecodedfile.txtwith the name you want for the decoded output.Example
Here’s a complete example:
-
Create a file:
bashecho "Hello, World!" > example.txt -
Encode the file in Base64:
bashbase64 example.txt > encoded_example.txt -
Decode the encoded file back to the original:
bashbase64 -d encoded_example.txt > decoded_example.txt -
Check the original and decoded files:
bashcat example.txt cat decoded_example.txt
Both
catcommands should show the same content: "Hello, World!" -