# 管理與操作

#### 基本操作

查出目前使用哪種 SHELL

```shell
ps -p $$
echo $0
echo $SHELL 
```

執行 SHELL 的方式

```shell
sh file
. file
source file
```

執行前一個指令的參數

```bash
ls -l /home/alang/test.py
vi !$
# 或
# 適用 ksh, bash, fish, zsh
vi $_
# 或
vi <alt> + .
```

執行前一個指令

```
!!
```

搜尋最近曾執行過的指令

```
<ctrl> + r
(reverse-i-search): 輸入指令開頭的前幾個字元

```

搜尋所有曾執行過的指令集

```part
$> history | grep "keyword"
```


#### Alias

以下內容需要編輯 ~/.bashrc  
使用方法

```shell
# User specific aliases and functions
alias date-time='date && cal'
```

##### 常用 Aliases

```shell
# Custom specified aliases
alias lastmod="find . -type f -exec stat --format '%Y :%y %n' \"{}\" \; | sort -nr | cut -d: -f2-"
alias vi="vim"
alias grep="grep --color"
```

##### 自訂 functions

```shell
date-time () {
    date && cal
    return
}

export -f date-time
```

避免誤用 crontab 清除指令

```bash
# Avoid crontab deleted by mistake with the command 'crontab -r'.
# shift 1: get rid of the first one argument
crontab() {
    case $* in
      -r* ) shift 1; echo -n "Really delete your crontab? (y/n)? "; read ans; if [ "$ans" = "y" ]; then command crontab -r; else echo "Canceled."; fi;;
        * ) command crontab "$@";;
    esac
}
```

管理自訂函式

```bash
# List All Functions
declare -F

# View a specific function
declare -F function-name

# Delete a specific function
unset -f function-name
```

#### Custom Prompt

- [Customizing Your Bash Prompt](https://www.notion.so/Customizing-Your-Bash-Prompt-ef312daad923421c859bda89a5dd1792)
- [PS1 Customization](http://bashrcgenerator.com/)

.bashrc:

```bash
# Kali-like Prompt
if $(__git_ps1 2>/dev/null);then
    PS1="\[\033[38;5;209m\]┌──[\[\033[38;5;141m\]\u\[\033[38;5;209m\]@\[\033[38;5;105m\]\h\[\033[38;5;231m\]:\w\[\033[38;5;209m\]]\[\033[33m\]\$(GIT_PS1_SHOWUNTRACKEDFILES=1 GIT_PS1_SHOWDIRTYSTATE=1 __git_ps1)\[\033[00m\]\n\[\033[38;5;209m\]└─\\[\033[38;5;209m\]\\$\[\033[37m\] "
else
    source /usr/share/git-core/contrib/completion/git-prompt.sh
    PS1="\[\033[38;5;209m\]┌──[\[\033[38;5;141m\]\u\[\033[38;5;209m\]@\[\033[38;5;105m\]\h\[\033[38;5;231m\]:\w\[\033[38;5;209m\]]\[\033[33m\]\$(GIT_PS1_SHOWUNTRACKEDFILES=1 GIT_PS1_SHOWDIRTYSTATE=1 __git_ps1)\[\033[00m\]\n\[\033[38;5;209m\]└─\\[\033[38;5;209m\]\\$\[\033[37m\] "
fi
```

```
┌──[alang@mint-HX90:~]
└─$ 
```

#### 其他

##### Formatting Scripts

```shell
# Install shfmt
## On Ubuntu
sudo snap install shfmt
## On Alpine Linux
sudo apk add shfmt
## On FreeBSD
sudo pkg install devel/shfmt

# Format shell programs using Shfmt
## -i flag is the amount of spaces that will be used to intend.
shfmt -i 4 myscript.sh
## With Diff style output
shfmt -d -i 4 myscript.sh
```

##### 終端機輸出內容轉存一個檔案

- [How to Save the Terminal Output to a File in Linux - Make Tech Easier](https://www.maketecheasier.com/save-output-of-command-to-file-linux/)

```bash
# 1. script
# Once you are done with the session, type the 'exit'.
script my.out

# 2. tee
myprogram | tee my.out
```

#### 常用鍵盤快捷鍵

<table class="part" data-endline="13" data-startline="6" id="bkmrk-key-operation-ctrl-%2B"><thead><tr><th>Key</th><th>Operation</th></tr></thead><tbody><tr><td>Ctrl + a</td><td>Go to the **beginning** of the line.</td></tr><tr><td>Ctrl + e</td><td>Go to the **end** of the line.</td></tr><tr><td>Alt + b</td><td>Go left(**back**) one word.</td></tr><tr><td>Alt + f</td><td>Go right(**forward**) one word.</td></tr><tr><td>Alt + .</td><td>Use the **last word** of the previous command.</td></tr><tr><td>Ctrl + r</td><td>**Search** through the command's history</td></tr><tr><td>Ctrl + u</td><td>Cut the part of the line **before** the cursor, adding it to the clipboard.</td></tr><tr><td>Ctrl + k</td><td>Cut the part of the line **after** the cursor, adding it to the clipboard.</td></tr><tr><td>Ctrl + l</td><td>Clear the screen</td></tr><tr><td>Ctrl + w</td><td>刪除游標前個單字</td></tr><tr><td>Ctrl+x,Ctrl+e</td><td>Edit the current command in your **$EDITOR**.</td></tr></tbody></table>

[![Linux_terminal_shortcuts.jpeg](https://osslab.tw/uploads/images/gallery/2023-04/scaled-1680-/linux-terminal-shortcuts.jpeg)](https://osslab.tw/uploads/images/gallery/2023-04/linux-terminal-shortcuts.jpeg)