install.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/bin/sh
  2. #
  3. # This script should be run via curl:
  4. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  5. # or via wget:
  6. # sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  7. # or via fetch:
  8. # sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  9. #
  10. # As an alternative, you can first download the install script and run it afterwards:
  11. # wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
  12. # sh install.sh
  13. #
  14. # You can tweak the install behavior by setting variables when running the script. For
  15. # example, to change the path to the Oh My Zsh repository:
  16. # ZSH=~/.zsh sh install.sh
  17. #
  18. # Respects the following environment variables:
  19. # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
  20. # REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
  21. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  22. # BRANCH - branch to check out immediately after install (default: master)
  23. #
  24. # Other options:
  25. # CHSH - 'no' means the installer will not change the default shell (default: yes)
  26. # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
  27. # KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
  28. #
  29. # You can also pass some arguments to the install script to set some these options:
  30. # --skip-chsh: has the same behavior as setting CHSH to 'no'
  31. # --unattended: sets both CHSH and RUNZSH to 'no'
  32. # --keep-zshrc: sets KEEP_ZSHRC to 'yes'
  33. # For example:
  34. # sh install.sh --unattended
  35. # or:
  36. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
  37. #
  38. set -e
  39. # Track if $ZSH was provided
  40. custom_zsh=${ZSH:+yes}
  41. # Default settings
  42. ZSH=${ZSH:-~/.oh-my-zsh}
  43. REPO=${REPO:-ohmyzsh/ohmyzsh}
  44. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  45. BRANCH=${BRANCH:-master}
  46. # Other options
  47. CHSH=${CHSH:-yes}
  48. RUNZSH=${RUNZSH:-yes}
  49. KEEP_ZSHRC=${KEEP_ZSHRC:-no}
  50. command_exists() {
  51. command -v "$@" >/dev/null 2>&1
  52. }
  53. fmt_error() {
  54. echo ${RED}"Error: $@"${RESET} >&2
  55. }
  56. fmt_underline() {
  57. echo "$(printf '\033[4m')$@$(printf '\033[24m')"
  58. }
  59. fmt_code() {
  60. echo "\`$(printf '\033[38;5;247m')$@${RESET}\`"
  61. }
  62. setup_color() {
  63. # Only use colors if connected to a terminal
  64. if [ -t 1 ]; then
  65. RED=$(printf '\033[31m')
  66. GREEN=$(printf '\033[32m')
  67. YELLOW=$(printf '\033[33m')
  68. BLUE=$(printf '\033[34m')
  69. BOLD=$(printf '\033[1m')
  70. RESET=$(printf '\033[m')
  71. else
  72. RED=""
  73. GREEN=""
  74. YELLOW=""
  75. BLUE=""
  76. BOLD=""
  77. RESET=""
  78. fi
  79. }
  80. setup_ohmyzsh() {
  81. # Prevent the cloned repository from having insecure permissions. Failing to do
  82. # so causes compinit() calls to fail with "command not found: compdef" errors
  83. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  84. # that this will be ignored under Cygwin by default, as Windows ACLs take
  85. # precedence over umasks except for filesystems mounted with option "noacl".
  86. umask g-w,o-w
  87. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  88. command_exists git || {
  89. fmt_error "git is not installed"
  90. exit 1
  91. }
  92. if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
  93. fmt_error "Windows/MSYS Git is not supported on Cygwin"
  94. fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  95. exit 1
  96. fi
  97. git clone -c core.eol=lf -c core.autocrlf=false \
  98. -c fsck.zeroPaddedFilemode=ignore \
  99. -c fetch.fsck.zeroPaddedFilemode=ignore \
  100. -c receive.fsck.zeroPaddedFilemode=ignore \
  101. --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  102. fmt_error "git clone of oh-my-zsh repo failed"
  103. exit 1
  104. }
  105. echo
  106. }
  107. setup_zshrc() {
  108. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  109. # with datestamp of installation that moved them aside, so we never actually
  110. # destroy a user's original zshrc
  111. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  112. # Must use this exact name so uninstall.sh can find it
  113. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  114. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  115. # Skip this if the user doesn't want to replace an existing .zshrc
  116. if [ $KEEP_ZSHRC = yes ]; then
  117. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
  118. return
  119. fi
  120. if [ -e "$OLD_ZSHRC" ]; then
  121. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  122. if [ -e "$OLD_OLD_ZSHRC" ]; then
  123. fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  124. fmt_error "re-run the installer again in a couple of seconds"
  125. exit 1
  126. fi
  127. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  128. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  129. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  130. fi
  131. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  132. mv ~/.zshrc "$OLD_ZSHRC"
  133. fi
  134. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  135. sed "/^export ZSH=/ c\\
  136. export ZSH=\"$ZSH\"
  137. " "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
  138. mv -f ~/.zshrc-omztemp ~/.zshrc
  139. echo
  140. }
  141. setup_shell() {
  142. # Skip setup if the user wants or stdin is closed (not running interactively).
  143. if [ $CHSH = no ]; then
  144. return
  145. fi
  146. # If this user's login shell is already "zsh", do not attempt to switch.
  147. if [ "$(basename -- "$SHELL")" = "zsh" ]; then
  148. return
  149. fi
  150. # If this platform doesn't provide a "chsh" command, bail out.
  151. if ! command_exists chsh; then
  152. cat <<EOF
  153. I can't change your shell automatically because this system does not have chsh.
  154. ${BLUE}Please manually change your default shell to zsh${RESET}
  155. EOF
  156. return
  157. fi
  158. echo "${BLUE}Time to change your default shell to zsh:${RESET}"
  159. # Prompt for user choice on changing the default login shell
  160. printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
  161. read opt
  162. case $opt in
  163. y*|Y*|"") echo "Changing the shell..." ;;
  164. n*|N*) echo "Shell change skipped."; return ;;
  165. *) echo "Invalid choice. Shell change skipped."; return ;;
  166. esac
  167. # Check if we're running on Termux
  168. case "$PREFIX" in
  169. *com.termux*) termux=true; zsh=zsh ;;
  170. *) termux=false ;;
  171. esac
  172. if [ "$termux" != true ]; then
  173. # Test for the right location of the "shells" file
  174. if [ -f /etc/shells ]; then
  175. shells_file=/etc/shells
  176. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  177. shells_file=/usr/share/defaults/etc/shells
  178. else
  179. fmt_error "could not find /etc/shells file. Change your default shell manually."
  180. return
  181. fi
  182. # Get the path to the right zsh binary
  183. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  184. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  185. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  186. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  187. fmt_error "no zsh binary found or not present in '$shells_file'"
  188. fmt_error "change your default shell manually."
  189. return
  190. fi
  191. fi
  192. fi
  193. # We're going to change the default shell, so back up the current one
  194. if [ -n "$SHELL" ]; then
  195. echo $SHELL > ~/.shell.pre-oh-my-zsh
  196. else
  197. grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  198. fi
  199. # Actually change the default shell to zsh
  200. if ! chsh -s "$zsh"; then
  201. fmt_error "chsh command unsuccessful. Change your default shell manually."
  202. else
  203. export SHELL="$zsh"
  204. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  205. fi
  206. echo
  207. }
  208. main() {
  209. # Run as unattended if stdin is not a tty
  210. if [ ! -t 0 ]; then
  211. RUNZSH=no
  212. CHSH=no
  213. fi
  214. # Parse arguments
  215. while [ $# -gt 0 ]; do
  216. case $1 in
  217. --unattended) RUNZSH=no; CHSH=no ;;
  218. --skip-chsh) CHSH=no ;;
  219. --keep-zshrc) KEEP_ZSHRC=yes ;;
  220. esac
  221. shift
  222. done
  223. setup_color
  224. if ! command_exists zsh; then
  225. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  226. exit 1
  227. fi
  228. if [ -d "$ZSH" ]; then
  229. echo "${YELLOW}The \$ZSH folder already exists ($ZSH).${RESET}"
  230. if [ "$custom_zsh" = yes ]; then
  231. cat <<EOF
  232. You ran the installer with the \$ZSH setting or the \$ZSH variable is
  233. exported. You have 3 options:
  234. 1. Unset the ZSH variable when calling the installer:
  235. $(fmt_code "ZSH= sh install.sh")
  236. 2. Install Oh My Zsh to a directory that doesn't exist yet:
  237. $(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")
  238. 3. (Caution) If the folder doesn't contain important information,
  239. you can just remove it with $(fmt_code "rm -r $ZSH")
  240. EOF
  241. else
  242. echo "You'll need to remove it if you want to reinstall."
  243. fi
  244. exit 1
  245. fi
  246. setup_ohmyzsh
  247. setup_zshrc
  248. setup_shell
  249. printf "$GREEN"
  250. cat <<'EOF'
  251. __ __
  252. ____ / /_ ____ ___ __ __ ____ _____/ /_
  253. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  254. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  255. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  256. /____/ ....is now installed!
  257. EOF
  258. cat <<EOF
  259. Before you scream Oh My Zsh! please look over the ~/.zshrc file to select plugins, themes, and options.
  260. • Follow us on Twitter: $(fmt_underline https://twitter.com/ohmyzsh)
  261. • Join our Discord server: $(fmt_underline https://discord.gg/ohmyzsh)
  262. • Get stickers, shirts, coffee mugs and other swag: $(fmt_underline https://shop.planetargon.com/collections/oh-my-zsh)
  263. EOF
  264. printf "$RESET"
  265. if [ $RUNZSH = no ]; then
  266. echo "${YELLOW}Run zsh to try it out.${RESET}"
  267. exit
  268. fi
  269. exec zsh -l
  270. }
  271. main "$@"