install.sh 8.7 KB

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