리눅스2024. 1. 8. 08:52

 

리눅스에서 베이스64(base64) 인코딩을 한 결과를 한줄로 출력하는 방법이다. 별도의 옵션을 지정하지 않으면 결과값이 길 경우 줄바꿈이 이루어진다.

# 긴 문자열에 대한 베이스64 인코딩 예시
echo "Hello. This is John speaking. May I speak to Jane please?" | base64
SGVsbG8uIFRoaXMgaXMgSm9obiBzcGVha2luZy4gTWF5IEkgc3BlYWsgdG8gSmFuZSBwbGVhc2U/
Cg==

예전에 tr과 sed를 이용해서 위와 같은 출력을 한줄로 바꾸는 방법을 정리했던 적이 있는데(여기로), 알고 보니 base64 자체 옵션(-w 0)을 이용하면 간단하게 해결된다.

# 출력의 줄바꿈 비활성화(-w 0 옵션)
echo "Hello. This is John speaking. May I speak to Jane please?" | base64 -w 0
SGVsbG8uIFRoaXMgaXMgSm9obiBzcGVha2luZy4gTWF5IEkgc3BlYWsgdG8gSmFuZSBwbGVhc2UCg==

728x90
Posted by 반달가면