# Shell

## bash basics

### default text editor
* for inline shell call: `sudo EDITOR=nano crontab -e`


* add to `~/.bashrc` or `~/.zshrc`:
    * `export EDITOR=nano`
* for default root editor, edit also `/root/.bashrc` or `/root/.zshrc` 

```
echo "export VISUAL=nano\nexport EDITOR=nano" >> ~/.bashrc
echo "export VISUAL=nano\nexport EDITOR=nano" >> ~/.zshrc

sudo sh -c "echo -e \"export VISUAL=nano\nexport EDITOR=nano\" >> ~/.bashrc"
sudo sh -c "echo -e \"export VISUAL=nano\nexport EDITOR=nano\" >> ~/.zshrc"
```

### login message:

* for local user edit: `nano ~/.profile`
* for global: `/etc/profile` or `/etc/bashrc` (only for bash shell)

add for example:
```bash
#shell login message
echo -e "\n\n You are working on: \t\e[41m>>> subdomain.my-server.com <<<\e[49m\n\n"
```
### custom shell prompt:

* for local user edit: `nano ~/.profile`
* for global: `/etc/profile` or `/etc/bashrc` (only for bash shell)

add for example:
```bash
#shell login message
alias PS1="\e[0;32m[\u@\h \w]\$ \e[0m"
```

* `\u` - user
* `\h` - host
* `\w` - current path


`[test@ubuntu1 ~]$` <- in green

#### `PS3` for select prompt
[https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/](https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/)

### Terminal colors
* `\e[0;32m`

* `\e[` – Begin color changes
* `0;32m` – Specify the color code
* `[\u@\h \W]\$` – This is the code for your normal BASH prompt (username@hostname Workingdirectory $)
* `\e[0m` – Exit color-change mode

The first number in the color code specifies the typeface:

* `0` – Normal
* `1` – Bold (bright)
* `2` – Dim
* `4` – Underlined

The second number indicates the color you want:

| color      | background | text     |
|:---------- |:---------- |:-------- |
| black      | \e[0;40m   | \e[0;30m |
| Red        | \e[0;41m   | \e[0;31m |
| Green      | \e[0;42m   | \e[0;32m |
| Brown      | \e[0;43m   | \e[0;33m |
| Blue       | \e[0;44m   | \e[0;34m |
| Purple     | \e[0;45m   | \e[0;35m |
| Cyan       | \e[0;46m   | \e[0;36m |
| Light gray | \e[0;47m   | \e[0;37m |


### output redirection

* `2>&1` redirect StdErr to StdOut
* `2>&1 |` redirect and Pipe, same as `|&` (not in sh)

```
          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file   
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append
```


### zsh
#### all in one script

```bash
if test -f ~/.zshrc; then
  echo "Installing zsh with oh my zsh and powerlevel10kTheme"

  sudo apt update && sudo apt install curl git zsh
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
  git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  sed -i '/ZSH_THEME="/c\ZSH_THEME="powerlevel10k/powerlevel10k"' ~/.zshrc
fi
```

To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.

#### install zsh

* ZSH (shell with more features)
  * `sudo apt install zsh` or `sudo dnf intall zsh` (on Fedora)
* Oh-My-ZSH
  * `sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"`

Oh My Zsh [Cheatsheet](https://github.com/ohmyzsh/ohmyzsh/wiki/Cheatsheet)

##### install powerlevel 10k theme
* install font for "powerlevel10k"
  [https://github.com/romkatv/powerlevel10k#meslo-nerd-font-patched-for-powerlevel10k](https://github.com/romkatv/powerlevel10k#meslo-nerd-font-patched-for-powerlevel10k)
* **choose font `Meslo Nerd` in preferences of the terminal or editor**
* install "powerlevel10k"-theme for ohmyzsh https://github.com/romkatv/powerlevel10k
  `git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k`
* Set `ZSH_THEME="powerlevel10k/powerlevel10k"` in `~/.zshrc`.
    * `sed -i '/ZSH_THEME="/c\ZSH_THEME="powerlevel10k/powerlevel10k"' ~/.zshrc`
* Type `p10k configure` if the configuration wizard doesn't start automatically.

###### edit for visual studio code

* Press `command` + `shift` + `P` in your VSCode window.
* type `settings.json` and click "Open User Settings (JSON)" it.
* insert or add:

  ```json
  {
    "terminal.integrated.fontFamily": "MesloLGS NF",
    "terminal.integrated.fontSize": 12,
    "terminal.integrated.defaultProfile.linux": "zsh"
  }
  ```


### terminal emulator


#### terminator

* install: `sudo apt install terminator`

##### documentation
* Create more terminals by:
  * horizontal split: `Ctrl-Shift-o`
  * vertical split: `Ctrl-Shift-e`
* Shift focus to:
  * next terminal: `Ctrl-Shift-n`
  * previous terminal: `Ctrl-Shift-p`
* New tab: `Ctrl-Shift-t`
* New window: `Ctrl-Shift-i`
* Close terminal or tab:
  * `Ctrl-Shift-w`
  * or right mouse click -> Close
* Close window with all it's terminals and tabs: `Ctrl-Shift-q`
* Reset zoom: `Ctrl-0`