# Linux-tips

Last updated: 2023-02-23

```bash
# sudo no password
echo "$(whoami)" ALL=(ALL) NOPASSWD:ALL >> /etc/sudoers
```

```bash
# copy ssh keys
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host
ssh-copy-id -f -i ~/.ssh/id_rsa.pub user@host # force add without checking if key exists
# not fancy way
ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
```

VIM

```bash
# delete a world: `daw`
# delete a sentence: `das`
#  __d__elete, __y__ank, __c__hange, > (indent in)
# b for before, e for end, a for append, i for insert.
# caw - perform the c operator on the aw text object (ergo, change the "a word" that the cursor is currently on).
# dd for delete line, yy for copy line, cc for change line
# >> for align line right, << for align line left
# :%s/old/new/g for replace all
# :%s/old/new/gc for replace all with confirm
# :%s/old/new/gcI for replace all with confirm and ignore case
# BdW (go to first whitespace delete to next whitespace)
# bdw, back delete word.
# Scroll: Ctrl-u (up), Ctrl-d (down)
# Find: f{character}, t{character}, F{character}, T{character}
# find/to forward/backward {character} on the current line
# , / ; for navigating matches
# Search: /{regex}, n / N for navigating matches
# %s/foo/bar/g replace foo with bar globally in file
# To switch to the right window, press “Ctrl + w”, then “l”. To go to the left window, it's “Ctrl + w”, then “h”. If you did a horizontal split, then going up and down is necessary. For going up, press “Ctrl + w”, then “k”. For going down, press “Ctrl + w”, then “j”.
# set :nu for line numbers

```

BASH

```bash
# $_ - Last argument from the last command
# $! - PID of the last background command
# $@ - All arguments
# $* - All arguments, unquoted
# $? - Exit status of the last command
# $$ - PID of the current shell
# $0 - Name of the current script
# $# - Number of arguments passed to script
# !! - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doing sudo !!
# ESC . - last argument from the previous command
# !:n - nth argument from the previous command

```

```bash
bash -c 'cat < "TEST"'
#bash: TEST: No such file or directory means that the shell is trying to read from a file called TEST
bash -c 'cat << "TEST"'
#bash: warning: here-document at line 0 delimited by end-of-file (wanted `TEST') means that the shell is trying to read from here-document 
bash -c 'cat <<< "TEST"'
#TEST means that the shell is trying to read from here-string
```

```bash
# process substitution
# <(command) - read from the output of command
# >(command) - write to the input of command
```

```bash
# bash coproc (> bash 4.0)
# coproc NAME { command; }
# $NAME_PID - PID of the coprocess
# ${NAME[@]} - file descriptor of the coprocess
# ${NAME[0]} - ouput file descriptor of the coprocess
# ${NAME[1]} - input file descriptor of the coprocess

set -e
set -x

coproc macaroons (echo $(date))

echo "The coprocess array: ${macaroons[@]}"
echo "The PID of the coprocess is ${macaroons_PID}"
# Read the output off the first file descriptor of the array.                                                                                
read -r output <&"${macaroons[0]}" # read -r -u "${macaroons[0]}" output
echo "The output of the coprocess is ${output}"
# https://copyconstruct.medium.com/bash-coprocess-2092a93ad912
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.junyangz.com/ops/linux-tips.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
