Bash yes/no prompt function and password prompt with input masking

# Bash Y/n ask function
# Use: 
# if ask "Do you want to continue?"; then
#   echo "Yes"
# else
#   echo "No"
# fi
function ask {  
  read -p "$1 [Y/n] " -n 1 -r
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    return 0
  elif [[ $REPLY = '' ]]; then
    return 0
  else
    echo
    return 1
  fi
}

Prompt for password with input masking:

# Bash password prompt function with input masking
# Use:
# ask_pass "Enter the password: "
# password="$REPLY"
function ask_pass {
  REPLY=""
  prompt="$1"
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
    if [[ $char == $'\0' ]]; then
      break
    fi
    prompt='*'
    REPLY+="$char"
  done
  echo
}

Zsh + Oh My Zsh + themes + powerlevel10k + plugins / bash replacement

Zsh

Install zsh:

sudo apt-get install zsh

Change the user default shell to zsh:

chsh -s $(which zsh)

Until reboot you can enter the zsh with command zsh.

If you are working in some desktop environment like Gnome you can log out and log in to save the change of default shell.

OMZ

Install Oh My Zsh:

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

Install the recommended font to show icons in command prompt (or any of recommended fonts):

Download and copy to ~/.local/share/fonts

Code:

mkdir -p ~/.local/share/fonts
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf

Set the font in your terminal emulator settings to make it work.

Now you can play with omz command. Just type omz to get available options.

You can change theme to e.g. omz theme set simple etc.

Powerlevel10k

Optionally you can install one of best custom theme which is Powerlevel10k.

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Set theme:

omz theme set powerlevel10k/powerlevel10k

For the first theme set, the configurator should run itself.

Once you are in powerlevel10k theme you can reconfigure it with p10k configure command.

Plugins

Now you can set plugins. Plugin list with command omz plugin list.

Recommended plugins:

  • git (set by default)
  • autosuggestions (zsh-autosuggestions)
    • install with git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    • Enable with the command omz plugin enable zsh-autosuggestions

Sources

Showing git branch in bash prompt

To achieve something like this:

user@machine:~/myproject (develop)$

In .bashrc file at the end we need to add:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
# without host
#PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "