Skip to content

Instantly share code, notes, and snippets.

@holly
Last active March 1, 2026 16:37
Show Gist options
  • Select an option

  • Save holly/e080372da4e8ca9aad7259540acd9077 to your computer and use it in GitHub Desktop.

Select an option

Save holly/e080372da4e8ca9aad7259540acd9077 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
set -C
DOWNLOAD_URL="https://bitwarden.com/download/?app=cli&platform=linux"
INSTALL_DIR="$HOME/.local/bin"
INSTALL_PATH="${INSTALL_DIR}/bw"
_execute() {
if [ $# -ne 1 ]; then
REDIRECT_URL=$(_redirect_url)
fi
TMP_ZIPNAME=$(_make_tmp_zipname $REDIRECT_URL)
curl -sfL -o $TMP_ZIPNAME $REDIRECT_URL
unzip -d $INSTALL_DIR $TMP_ZIPNAME
rm -frv $(dirname $TMP_ZIPNAME)
}
_make_tmp_zipname() {
URL=$1
echo "$(mktemp -d)/$(basename $URL)"
}
_redirect_url() {
curl -w "%{redirect_url}" -s -o /dev/null "$DOWNLOAD_URL"
}
_remote_version_from_redirect_url() {
URL=$1
echo $URL | perl -nlpe 's/^.*\/bw\-linux\-(.*)\.zip$/$1/'
}
_local_version() {
$INSTALL_PATH --version
}
install() {
if [[ -f $INSTALL_PATH ]]; then
echo "$INSTALL_PATH is already exists."
exit 1
fi
_execute
}
forceinstall() {
uninstall
echo "bw force install."
echo ""
install
}
update() {
if [[ ! -f $INSTALL_PATH ]]; then
echo "$INSTALL_PATH is not exists."
exit 1
fi
LOCAL_VERSION=$(_local_version)
REDIRECT_URL=$(_redirect_url)
REMOTE_VERSION="$(_remote_version_from_redirect_url $REDIRECT_URL)"
if [[ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]]; then
echo "$INSTALL_PATH version $REMOTE_VERSION is already installed."
exit 1
fi
_execute $REDIRECT_URL
}
uninstall() {
if [[ ! -f $INSTALL_PATH ]]; then
echo "$INSTALL_PATH is not exists."
return
fi
rm -fv $INSTALL_PATH
}
usage() {
echo "Usage: $0 [install|forceinstall|update|uninstall]"
exit 1
}
if [ $# -ne 1 ]; then
usage
fi
COMMAND=$1
case "$COMMAND" in
"install" ) install ;;
"forceinstall" ) forceinstall ;;
"update" ) update ;;
"uninstall" ) uninstall ;;
* ) usage;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment