# Bash

## History

**Ctrl+R** will open the search history window.  
You may now use Ctrl+R again to search for the previous match.

## Edit current line in EDITOR

**Ctrl-X**, **Ctrl-E**: will open the current line in EDITOR.

## Terminal title

```sh
echo -ne "\033]0;MY_NEW_TITLE\007"
```

## Arrays

```bash
    arr=(1 2 3)
    echo ${arr[0]}
    echo ${arr[@]}
    echo ${arr[*]}
    echo ...
    copy=("${arr[@]}")
```

## Cursor

### Hide cursor

tput civis

#### Cursor movement

- Position the Cursor:
  \033[<L>;<C>H
     Or
  \033[<L>;<C>f
  puts the cursor at line L and column C.
- Move the cursor up N lines:
  \033[<N>A
- Move the cursor down N lines:
  \033[<N>B
- Move the cursor forward N columns:
  \033[<N>C
- Move the cursor backward N columns:
  \033[<N>D

- Clear the screen, move to (0,0):
  \033[2J
- Erase to end of line:
  \033[K

- Save cursor position:
  \033[s
- Restore cursor position:
  \033[u

## Escape

escape1() {
        rul1='s:\\:\\\\:g'
        rul2="s:':'\\\\'':g"
        /bin/echo -nE "$1" | sed "$rul1;$rul2;"
}

escape() {
        # bashism: last part is to "cancel" empty, single argument
        #builtin printf ' %q' "$@" | cut -c2- | sed "s:^''$::"

        # pure sh version for alpine and zimit and...
        if [ "$*" != "" ]; then
                while [ $# -gt 0 ]; do
                        /bin/echo -nE "'`escape1 "$1"`' "
                        shift
                done
        fi | sed 's: $::'

        echo
}
