Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Last active March 1, 2023 22:19
Show Gist options
  • Save maxpoletaev/4ed25183427a2cd7e57a to your computer and use it in GitHub Desktop.
Save maxpoletaev/4ed25183427a2cd7e57a to your computer and use it in GitHub Desktop.
Cross platform sed
#!/bin/bash
case "$OSTYPE" in
darwin*) PLATFORM="OSX" ;;
linux*) PLATFORM="LINUX" ;;
bsd*) PLATFORM="BSD" ;;
*) PLATFORM="UNKNOWN" ;;
esac
replace() {
if [[ "$PLATFORM" == "OSX" || "$PLATFORM" == "BSD" ]]; then
sed -i "" "$1" "$2"
elif [ "$PLATFORM" == "LINUX" ]; then
sed -i "$1" "$2"
fi
}
replace "s/foo/bar/g" file.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment