# Bash Tricks

## Three-Fingered Claw technique

These functions are \*NIX OS and shell flavor-robust. Put them at the beginning of your script (bash or otherwise), try() your statement and code on.

Explanation (based on flying sheep comment).

* yell: print the script name and all arguments to stderr:&#x20;
  * $0 is the path to the script ;
  * $\* are all arguments.
  * \>&2 means > redirect stdout to & pipe 2. pipe 1 would be stdout itself.
* die does the same as yell, but exits with a non-0 exit status, which means “fail”.
* try uses the || (boolean OR), which only evaluates the right side if the left one failed.
  * $@ is all arguments again, but different.

```
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
```

## Pipe Output to Arguments

Here is a short-liner which will prepend piped arguments to your script arguments list:

```
#!/bin/bash
args=$@
[[ -p /dev/stdin ]] && { mapfile -t; set -- "${MAPFILE[@]}"; set -- $@ $args; }

echo $@
```

Example use:

```
$ ./script.sh arg1 arg2 arg3
> arg1 arg2 arg3

$ echo "piped1 piped2 piped3" | ./script.sh
> piped1 piped2 piped3

$ echo "piped1 piped2 piped3" | ./script.sh arg1 arg2 arg3
> piped1 piped2 piped3 arg1 arg2 arg3


# https://superuser.com/questions/461946/can-i-use-pipe-output-as-a-shell-script-argument
```

## Renaming files with mv

```
root@kali:~/.ssh# mkdir x
root@kali:~/.ssh# mv x{,_COMPLETE}
root@kali:~/.ssh# ls
x_COMPLETE
```

## Linking

### Hard Link

```
$ ln source_file link
```

### Soft/Symbolic Link

Soft linking is useful for adding programs to in PATH directories e.g. \~/bin/. Soft links are created with the `ln` command. For example, the following would create a soft link named `link1` to a file named `file1`, both in the current directory

```
$ ln -s file1 link1
$ ls -l file1 link1 # verify new soft link
-rw-r--r-- 1 veryv wheel 0 Mar 7 22:01 file1
lrwxr-xr-x 1 veryv wheel 5 Mar 7 22:01 link1 -> file1
```

## Unzip multiple ZIP files into their own directories

```
for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done
```


---

# 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://wiki.zacheller.dev/shell-scripting/bash-tricks.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.
