Last active
January 30, 2023 15:44
-
-
Save mikeslattery/5c60655478f76e26b9232aedc664eb7d to your computer and use it in GitHub Desktop.
Cross Platform Bash Snippet for Linux, WSL 1, Cygwin, Msys, and GitBash.
This file contains 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
#!/bin/bash | |
# shebang added only to appease shellcheck | |
# Cross-platform for bash. | |
# Worry less about when writting a script that must run on Windows and Linux. | |
# Meant to be used with "source" command in scripts or .bashrc/.zshrc | |
export USER="${USER:-${USERNAME:-$(whoami)}}" | |
export USERNAME="${USERNAME:-$USER}" | |
export HOSTNAME="${HOSTNAME:-${MACHINENAME:-$(hostname)}}" | |
# Partial x-platform support for: cygpath, xdg-open, winpty, sudo, cmd | |
if [[ "$(uname -s)" == "Linux" ]]; then | |
if [[ "$(uname -r)" == *-Microsoft ]]; then | |
# WSL 1 | |
if ! [[ -d /c ]]; then | |
echo "/c must be mounted to /mnt/c" | |
exit 1 | |
fi | |
export USERPROFILE="${USERPROFILE:-/c/Users/$USER}" | |
xdg-open() { cmd /c start "$1"; } | |
if ! command -v cmd &>/dev/null; then | |
cmd() { /c/Windows/System32/cmd.exe "$@"; } | |
fi | |
if ! command -v cygpath &>/dev/null; then | |
if command -v wslpath &>/dev/null; then | |
cygpath() { wslpath "$@"; } | |
else | |
cygpath() { | |
if [[ "$1" == "-w" ]]; then | |
shift | |
readlink -f "$@" | \ | |
sed -r '/^(\/mnt)?\/[a-z]\b/ { s|/^(\/mnt)?\/([a-z])\b|\U\2:|; s|/|\\|g; }' | |
else | |
printf "%s\n" "$@" | sed -r 's|^(.):|/\L\1|; s|\\|/|g;' | |
fi | |
} | |
fi | |
fi | |
# Bug workaround | |
nice() { | |
if [[ "$1" == "-n" ]]; then shift; shift; fi | |
"$@" | |
} | |
export -f nice | |
renice() { :; } | |
export -f renice | |
else | |
# Real Linux | |
if [[ -z "${SSH_CONNECTION:-}" ]]; then | |
xdg-open() { w3m "$1"; } | |
fi | |
export USERPROFILE="${USERPROFILE:-$HOME}" | |
cmd() { shift; "$@"; } | |
cygpath() { | |
if [[ "$1" == "-w" ]]; then shift; fi | |
readlink -f "$1" | |
} | |
if command -v podman &>/dev/null && ! command -v docker &>/dev/null; then | |
docker() { | |
PODMAN_USERNS=keep-id podman "$@" | |
} | |
fi | |
fi | |
winpty() { "$@"; } | |
else | |
# Msys / Cygwin | |
xdg-open() { cmd /c start "$1"; } | |
sudo() { "$@"; } | |
export MSYS_NO_PATHCONV=1 | |
export MSYS2_ARG_CONV_EXCL='*' | |
export COMPOSE_CONVERT_WINDOWS_PATHS=1 | |
if command -v docker &>/dev/null; then | |
# voodoo magic to make the tty work correctly | |
docker() { | |
realdocker="$(command -v docker)" | |
# --tty or -t requires winpty | |
#shellcheck disable=SC2140,SC1001,SC2068,SC2145,SC2027 | |
if printf "%s\0" "$@" | grep -ZE '^--tty|^-[^-].*t|^-t.*'; then | |
winpty /bin/bash -c "xargs -0a <(printf "%s\0" "$@") '$realdocker'" | |
else | |
"$realdocker" "$@" | |
fi | |
} | |
export docker | |
fi | |
fi | |
if [[ -n "$TIMEFORMAT" ]]; then | |
export TIME="$TIMEFORMAT" | |
export TIMEFMT="$TIMEFORMAT" | |
fi | |
# If an ssh connection, connect X back to the host (a MS-Windows X server) | |
[ -z "$SSH_CLIENT" ] || export DISPLAY="${SSH_CLIENT/ */}:0" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jmeubank Thanks for the fixes.
I can't remember why, but bash -c "xargs..." is necessary as is. It fixes some weirdness with winpty and Windows.