Yo! Base64 Encode logoYo! Base64 Encode
Bash / Linux

How to Base64 Encode Data in Bash Linux

Learn how to encode data using Base64 in Bash and Linux with practical examples. Includes command-line tools, shell scripting, and file processing techniques.

By Ishan Karunaratne3 min read

Master Base64 encoding in Linux using command-line tools and Bash scripts. This guide covers everything from basic commands to advanced scripting techniques.

Looking to decode strings instead? Check out our guide on How to Base64 Decode Data in Bash Linux.

Quick Start: Basic Command-Line Encoding

Here's the simplest way to encode text in Linux:

BASH
# Basic string encoding
echo "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQo=

# Decode to verify
echo "SGVsbG8sIFdvcmxkIQo=" | base64 -d
# Output: Hello, World!

Essential Base64 Commands

String Encoding

BASH
# Encode without newline
echo -n "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQ==

# Encode with wrapping
echo "Long text that should be wrapped" | base64 -w 40

# Encode preserving new lines
cat << EOF | base64
Line 1
Line 2
Line 3
EOF

File Encoding

BASH
# Encode a file
base64 input.txt > encoded.txt

# Encode and wrap output at 76 characters
base64 -w 76 input.txt > encoded.txt

# Encode without wrapping
base64 -w 0 input.txt > encoded.txt

Shell Functions for Base64 Encoding

Create these useful functions in your .bashrc or scripts:

BASH
#!/bin/bash

# Function to encode string with error handling
encode_string() {
    if [ -z "$1" ]; then
        echo "Error: Empty input" >&2
        return 1
    }
    
    local result
    result=$(echo -n "$1" | base64) || {
        echo "Error: Encoding failed" >&2
        return 1
    }
    
    echo "$result"
}

# Function to encode file with progress
encode_file() {
    local input_file="$1"
    local output_file="$2"
    
    # Check if input file exists
    if [ ! -f "$input_file" ]; then
        echo "Error: Input file not found: $input_file" >&2
        return 1
    }
    
    # Get file size
    local size=$(stat -f %z "$input_file" 2>/dev/null || stat -c %s "$input_file")
    local processed=0
    
    # Create named pipe for progress tracking
    local pipe=$(mktemp -u)
    mkfifo "$pipe"
    
    # Start encoding in background
    base64 "$input_file" > "$output_file" < "$pipe" &
    
    # Show progress
    while [ "$processed" -lt "$size" ]; do
        local chunk_size=8192
        if [ "$((processed + chunk_size))" -gt "$size" ]; then
            chunk_size=$((size - processed))
        fi
        
        dd if="$input_file" of="$pipe" bs=1 skip="$processed" count="$chunk_size" 2>/dev/null
        processed=$((processed + chunk_size))
        
        # Calculate percentage
        local percent=$((processed * 100 / size))
        echo -ne "Progress: $percent%\r"
    done
    
    echo -e "\nEncoding complete: $output_file"
    rm "$pipe"
}

Processing Multiple Files

BASH
#!/bin/bash

# Function to encode multiple files
encode_batch() {
    local input_dir="$1"
    local output_dir="$2"
    
    # Create output directory if it doesn't exist
    mkdir -p "$output_dir"
    
    # Counter for progress
    local total=$(find "$input_dir" -type f | wc -l)
    local current=0
    
    find "$input_dir" -type f | while read -r file; do
        current=$((current + 1))
        local filename=$(basename "$file")
        local output_file="$output_dir/${filename}.b64"
        
        echo -ne "Processing file $current/$total: $filename\r"
        
        # Encode file
        base64 "$file" > "$output_file"
    done
    
    echo -e "\nBatch processing complete"
}

# Function to encode with size limit
encode_with_limit() {
    local input="$1"
    local max_size=$((1024 * 1024)) # 1MB limit
    
    # Check file size
    if [ -f "$input" ]; then
        local size=$(stat -f %z "$input" 2>/dev/null || stat -c %s "$input")
        if [ "$size" -gt "$max_size" ]; then
            echo "Error: File too large (max: $max_size bytes)" >&2
            return 1
        fi
    fi
    
    # Encode with progress
    base64 "$input"
}

Advanced Techniques

Streaming Large Files

BASH
#!/bin/bash

stream_encode() {
    local input_file="$1"
    local output_file="$2"
    local chunk_size=8192
    
    # Create output file
    > "$output_file"
    
    # Process file in chunks
    local total_size=$(stat -f %z "$input_file" 2>/dev/null || stat -c %s "$input_file")
    local processed=0
    
    while [ "$processed" -lt "$total_size" ]; do
        # Read and encode chunk
        dd if="$input_file" bs="$chunk_size" skip="$((processed / chunk_size))" count=1 2>/dev/null |
        base64 >> "$output_file"
        
        processed=$((processed + chunk_size))
        
        # Show progress
        echo -ne "Progress: $((processed * 100 / total_size))%\r"
    done
    
    echo -e "\nStreaming complete: $output_file"
}

Parallel Processing

BASH
#!/bin/bash

parallel_encode() {
    local input_dir="$1"
    local output_dir="$2"
    local max_jobs=4
    
    # Create output directory
    mkdir -p "$output_dir"
    
    # Process files in parallel
    find "$input_dir" -type f | while read -r file; do
        # Wait if too many jobs are running
        while [ $(jobs | wc -l) -ge "$max_jobs" ]; do
            sleep 1
        done
        
        # Start encoding job in background
        (
            local filename=$(basename "$file")
            base64 "$file" > "$output_dir/${filename}.b64"
            echo "Completed: $filename"
        ) &
    done
    
    # Wait for all jobs to complete
    wait
    echo "All files processed"
}

Practical Use Cases

Creating Data URIs

BASH
#!/bin/bash

create_data_uri() {
    local file="$1"
    
    # Get MIME type
    local mime_type=$(file -b --mime-type "$file")
    
    # Create data URI
    echo -n "data:$mime_type;base64,"
    base64 -w 0 "$file"
}

# Example for images
create_image_uri() {
    local image="$1"
    
    case "${image##*.}" in
        jpg|jpeg) mime="image/jpeg" ;;
        png) mime="image/png" ;;
        gif) mime="image/gif" ;;
        *) echo "Unsupported format" >&2; return 1 ;;
    esac
    
    echo -n "data:$mime;base64,"
    base64 -w 0 "$image"
}

Secure Input Handling

BASH
#!/bin/bash

secure_encode() {
    local input="$1"
    
    # Check for empty input
    if [ -z "$input" ]; then
        echo "Error: Empty input" >&2
        return 1
    }
    
    # Check for maximum length
    if [ ${#input} -gt 1048576 ]; then # 1MB limit
        echo "Error: Input too large" >&2
        return 1
    }
    
    # Check for control characters
    if echo "$input" | grep -q "[[:cntrl:]]"; then
        echo "Error: Invalid characters in input" >&2
        return 1
    }
    
    # Encode safely
    echo -n "$input" | base64
}

Conclusion

Base64 encoding in Linux is straightforward using built-in tools and shell scripting. Whether you're working with strings, files, or streams, the commands and scripts provided here give you a solid foundation for handling Base64 encoding tasks efficiently.

Frequently Asked Questions

Q: How do I handle special characters in file names? A: Use proper quoting and the "${variable}" syntax to safely handle special characters in file names.

Q: What's the maximum file size I can encode? A: There's no built-in limit, but use the streaming approach for files larger than a few megabytes to manage memory usage.

Q: How do I preserve line breaks in the encoded output? A: Use base64 -w 0 to disable line wrapping, or specify a custom width with -w <number>.

Q: Can I encode binary files safely? A: Yes, the base64 command handles binary files correctly by default. Just ensure you don't accidentally modify the encoded data.

Q: How do I automate Base64 encoding in shell scripts? A: Use the functions provided above and integrate them into your scripts, adding error handling and progress reporting as needed.

Encode text to Base64 now , paste it into the free encoder and get the result instantly.
Ishan Karunaratne

Ishan Karunaratne

Software & DevOps engineer

I build and maintain Yo! Base64 Encode and write these guides from hands-on work with encoding in real systems, API payloads, JWTs, CI pipelines, and the occasional 2am debugging session.

More of my writing at techearl.com