install.sh 15 KB

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