Last active
June 1, 2025 06:20
-
-
Save yeiichi/a85cc0c43a751c9d69c4104e54453568 to your computer and use it in GitHub Desktop.
Remove BOM (Byte Order Mark) sequence from a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env sh | |
| HELP_MESSAGE="Usage: $(basename "$0") <file> | |
| Remove BOM (Byte Order Mark) sequence from a file. | |
| Arguments: | |
| <file> Input file to process | |
| Options: | |
| -h, --help Show this help message" | |
| BOM_CHAR="$(printf '\357\273\277')" # EF BB BF in octal | |
| show_help() { | |
| echo "$HELP_MESSAGE" | |
| exit 0 | |
| } | |
| remove_bom_from_file() { | |
| input_file="$1" | |
| output_file="${input_file%.*}-clean.${input_file##*.}" | |
| sed "1s/^$BOM_CHAR//" "$input_file" > "$output_file" | |
| } | |
| main() { | |
| [ "$1" = "-h" ] || [ "$1" = "--help" ] && show_help | |
| [ -z "$1" ] && { echo "Error: Missing input file"; show_help; } | |
| remove_bom_from_file "$1" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment