install.sh 17 KB

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