install.sh 8.6 KB

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