install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. # The [ -t 1 ] check only works when the function is not called from
  54. # a subshell (like in `$(...)` or `(...)`, so this hack redefines the
  55. # function at the top level to always return false when stdout is not
  56. # a tty.
  57. if [ -t 1 ]; then
  58. is_tty() {
  59. true
  60. }
  61. else
  62. is_tty() {
  63. false
  64. }
  65. fi
  66. # This function uses the logic from supports-hyperlinks[1][2], which is
  67. # made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
  68. # [1] https://github.com/zkat/supports-hyperlinks
  69. # [2] https://crates.io/crates/supports-hyperlinks
  70. #
  71. # Copyright (c) 2021 Kat Marchán
  72. #
  73. # Licensed under the Apache License, Version 2.0 (the "License");
  74. # you may not use this file except in compliance with the License.
  75. # You may obtain a copy of the License at
  76. #
  77. # http://www.apache.org/licenses/LICENSE-2.0
  78. #
  79. # Unless required by applicable law or agreed to in writing, software
  80. # distributed under the License is distributed on an "AS IS" BASIS,
  81. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  82. # See the License for the specific language governing permissions and
  83. # limitations under the License.
  84. supports_hyperlinks() {
  85. # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
  86. if [ -n "$FORCE_HYPERLINK" ]; then
  87. [ "$FORCE_HYPERLINK" != 0 ]
  88. return $?
  89. fi
  90. # If stdout is not a tty, it doesn't support hyperlinks
  91. is_tty || return 1
  92. # DomTerm terminal emulator (domterm.org)
  93. if [ -n "$DOMTERM" ]; then
  94. return 0
  95. fi
  96. # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
  97. if [ -n "$VTE_VERSION" ]; then
  98. [ $VTE_VERSION -ge 5000 ]
  99. return $?
  100. fi
  101. # If $TERM_PROGRAM is set, these terminals support hyperlinks
  102. case "$TERM_PROGRAM" in
  103. Hyper|iTerm.app|terminology|WezTerm) return 0 ;;
  104. esac
  105. # kitty supports hyperlinks
  106. if [ "$TERM" = xterm-kitty ]; then
  107. return 0
  108. fi
  109. # Windows Terminal or Konsole also support hyperlinks
  110. if [ -n "$WT_SESSION" ] || [ -n "$KONSOLE_VERSION" ]; then
  111. return 0
  112. fi
  113. return 1
  114. }
  115. # Adapted from code and information by Anton Kochkov (@XVilka)
  116. # Source: https://gist.github.com/XVilka/8346728
  117. supports_truecolor() {
  118. case "$COLORTERM" in
  119. truecolor|24bit) return 0 ;;
  120. esac
  121. case "$TERM" in
  122. iterm |\
  123. tmux-truecolor |\
  124. linux-truecolor |\
  125. xterm-truecolor |\
  126. screen-truecolor) return 0 ;;
  127. esac
  128. return 1
  129. }
  130. fmt_link() {
  131. # $1: text, $2: url, $3: fallback mode
  132. if supports_hyperlinks; then
  133. printf '\033]8;;%s\a%s\033]8;;\a\n' "$2" "$1"
  134. return
  135. fi
  136. case "$3" in
  137. --text) printf '%s\n' "$1" ;;
  138. --url|*) fmt_underline "$2" ;;
  139. esac
  140. }
  141. fmt_underline() {
  142. is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
  143. }
  144. # shellcheck disable=SC2016 # backtick in single-quote
  145. fmt_code() {
  146. is_tty && printf '`\033[2m%s\033[22m`\n' "$*" || printf '`%s`\n' "$*"
  147. }
  148. fmt_error() {
  149. printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2
  150. }
  151. setup_color() {
  152. # Only use colors if connected to a terminal
  153. if ! is_tty; then
  154. RAINBOW=""
  155. RED=""
  156. GREEN=""
  157. YELLOW=""
  158. BLUE=""
  159. BOLD=""
  160. RESET=""
  161. return
  162. fi
  163. if supports_truecolor; then
  164. RAINBOW="
  165. $(printf '\033[38;2;255;0;0m')
  166. $(printf '\033[38;2;255;97;0m')
  167. $(printf '\033[38;2;247;255;0m')
  168. $(printf '\033[38;2;0;255;30m')
  169. $(printf '\033[38;2;77;0;255m')
  170. $(printf '\033[38;2;168;0;255m')
  171. $(printf '\033[38;2;245;0;172m')
  172. "
  173. else
  174. RAINBOW="
  175. $(printf '\033[38;5;196m')
  176. $(printf '\033[38;5;202m')
  177. $(printf '\033[38;5;226m')
  178. $(printf '\033[38;5;082m')
  179. $(printf '\033[38;5;021m')
  180. $(printf '\033[38;5;093m')
  181. $(printf '\033[38;5;163m')
  182. "
  183. fi
  184. RED=$(printf '\033[31m')
  185. GREEN=$(printf '\033[32m')
  186. YELLOW=$(printf '\033[33m')
  187. BLUE=$(printf '\033[34m')
  188. BOLD=$(printf '\033[1m')
  189. RESET=$(printf '\033[0m')
  190. }
  191. setup_ohmyzsh() {
  192. # Prevent the cloned repository from having insecure permissions. Failing to do
  193. # so causes compinit() calls to fail with "command not found: compdef" errors
  194. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  195. # that this will be ignored under Cygwin by default, as Windows ACLs take
  196. # precedence over umasks except for filesystems mounted with option "noacl".
  197. umask g-w,o-w
  198. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  199. command_exists git || {
  200. fmt_error "git is not installed"
  201. exit 1
  202. }
  203. ostype=$(uname)
  204. if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then
  205. fmt_error "Windows/MSYS Git is not supported on Cygwin"
  206. fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  207. exit 1
  208. fi
  209. git clone -c core.eol=lf -c core.autocrlf=false \
  210. -c fsck.zeroPaddedFilemode=ignore \
  211. -c fetch.fsck.zeroPaddedFilemode=ignore \
  212. -c receive.fsck.zeroPaddedFilemode=ignore \
  213. -c oh-my-zsh.remote=origin \
  214. -c oh-my-zsh.branch="$BRANCH" \
  215. --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  216. fmt_error "git clone of oh-my-zsh repo failed"
  217. exit 1
  218. }
  219. echo
  220. }
  221. setup_zshrc() {
  222. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  223. # with datestamp of installation that moved them aside, so we never actually
  224. # destroy a user's original zshrc
  225. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  226. # Must use this exact name so uninstall.sh can find it
  227. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  228. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  229. # Skip this if the user doesn't want to replace an existing .zshrc
  230. if [ "$KEEP_ZSHRC" = yes ]; then
  231. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
  232. return
  233. fi
  234. if [ -e "$OLD_ZSHRC" ]; then
  235. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  236. if [ -e "$OLD_OLD_ZSHRC" ]; then
  237. fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  238. fmt_error "re-run the installer again in a couple of seconds"
  239. exit 1
  240. fi
  241. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  242. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  243. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  244. fi
  245. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  246. mv ~/.zshrc "$OLD_ZSHRC"
  247. fi
  248. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  249. # Replace $HOME path with '$HOME' in $ZSH variable in .zshrc file
  250. omz=$(echo "$ZSH" | sed "s|^$HOME/|\$HOME/|")
  251. sed "s|^export ZSH=.*$|export ZSH=\"${omz}\"|" "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
  252. mv -f ~/.zshrc-omztemp ~/.zshrc
  253. echo
  254. }
  255. setup_shell() {
  256. # Skip setup if the user wants or stdin is closed (not running interactively).
  257. if [ "$CHSH" = no ]; then
  258. return
  259. fi
  260. # If this user's login shell is already "zsh", do not attempt to switch.
  261. if [ "$(basename -- "$SHELL")" = "zsh" ]; then
  262. return
  263. fi
  264. # If this platform doesn't provide a "chsh" command, bail out.
  265. if ! command_exists chsh; then
  266. cat <<EOF
  267. I can't change your shell automatically because this system does not have chsh.
  268. ${BLUE}Please manually change your default shell to zsh${RESET}
  269. EOF
  270. return
  271. fi
  272. echo "${BLUE}Time to change your default shell to zsh:${RESET}"
  273. # Prompt for user choice on changing the default login shell
  274. printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \
  275. "$YELLOW" "$RESET"
  276. read -r opt
  277. case $opt in
  278. y*|Y*|"") echo "Changing the shell..." ;;
  279. n*|N*) echo "Shell change skipped."; return ;;
  280. *) echo "Invalid choice. Shell change skipped."; return ;;
  281. esac
  282. # Check if we're running on Termux
  283. case "$PREFIX" in
  284. *com.termux*) termux=true; zsh=zsh ;;
  285. *) termux=false ;;
  286. esac
  287. if [ "$termux" != true ]; then
  288. # Test for the right location of the "shells" file
  289. if [ -f /etc/shells ]; then
  290. shells_file=/etc/shells
  291. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  292. shells_file=/usr/share/defaults/etc/shells
  293. else
  294. fmt_error "could not find /etc/shells file. Change your default shell manually."
  295. return
  296. fi
  297. # Get the path to the right zsh binary
  298. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  299. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  300. if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then
  301. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -n 1) || [ ! -f "$zsh" ]; then
  302. fmt_error "no zsh binary found or not present in '$shells_file'"
  303. fmt_error "change your default shell manually."
  304. return
  305. fi
  306. fi
  307. fi
  308. # We're going to change the default shell, so back up the current one
  309. if [ -n "$SHELL" ]; then
  310. echo "$SHELL" > ~/.shell.pre-oh-my-zsh
  311. else
  312. grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  313. fi
  314. # Actually change the default shell to zsh
  315. if ! chsh -s "$zsh"; then
  316. fmt_error "chsh command unsuccessful. Change your default shell manually."
  317. else
  318. export SHELL="$zsh"
  319. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  320. fi
  321. echo
  322. }
  323. # shellcheck disable=SC2183 # printf string has more %s than arguments ($RAINBOW expands to multiple arguments)
  324. print_success() {
  325. printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET
  326. printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET
  327. printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $RAINBOW $RESET
  328. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET
  329. printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET
  330. printf '%s %s %s %s /____/ %s %s %s %s....is now installed!%s\n' $RAINBOW $GREEN $RESET
  331. printf '\n'
  332. printf '\n'
  333. printf "%s %s %s\n" "Before you scream ${BOLD}${YELLOW}Oh My Zsh!${RESET} look over the" \
  334. "$(fmt_code "$(fmt_link ".zshrc" "file://$HOME/.zshrc" --text)")" \
  335. "file to select plugins, themes, and options."
  336. printf '\n'
  337. printf '%s\n' "• Follow us on Twitter: $(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)"
  338. printf '%s\n' "• Join our Discord community: $(fmt_link "Discord server" https://discord.gg/ohmyzsh)"
  339. printf '%s\n' "• Get stickers, t-shirts, coffee mugs and more: $(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)"
  340. printf '%s\n' $RESET
  341. }
  342. main() {
  343. # Run as unattended if stdin is not a tty
  344. if [ ! -t 0 ]; then
  345. RUNZSH=no
  346. CHSH=no
  347. fi
  348. # Parse arguments
  349. while [ $# -gt 0 ]; do
  350. case $1 in
  351. --unattended) RUNZSH=no; CHSH=no ;;
  352. --skip-chsh) CHSH=no ;;
  353. --keep-zshrc) KEEP_ZSHRC=yes ;;
  354. esac
  355. shift
  356. done
  357. setup_color
  358. if ! command_exists zsh; then
  359. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  360. exit 1
  361. fi
  362. if [ -d "$ZSH" ]; then
  363. echo "${YELLOW}The \$ZSH folder already exists ($ZSH).${RESET}"
  364. if [ "$custom_zsh" = yes ]; then
  365. cat <<EOF
  366. You ran the installer with the \$ZSH setting or the \$ZSH variable is
  367. exported. You have 3 options:
  368. 1. Unset the ZSH variable when calling the installer:
  369. $(fmt_code "ZSH= sh install.sh")
  370. 2. Install Oh My Zsh to a directory that doesn't exist yet:
  371. $(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")
  372. 3. (Caution) If the folder doesn't contain important information,
  373. you can just remove it with $(fmt_code "rm -r $ZSH")
  374. EOF
  375. else
  376. echo "You'll need to remove it if you want to reinstall."
  377. fi
  378. exit 1
  379. fi
  380. setup_ohmyzsh
  381. setup_zshrc
  382. setup_shell
  383. print_success
  384. if [ $RUNZSH = no ]; then
  385. echo "${YELLOW}Run zsh to try it out.${RESET}"
  386. exit
  387. fi
  388. exec zsh -l
  389. }
  390. main "$@"