Shell
What is a shell?
What is it? A shell is a user-space program that reads text, expands it and executes programs, wiring up their file descriptors.
What is it really? A shell is a program that implements the POSIX spec that defines the interface and how it behaves.
zsh, bash, sh, etc. all implement the common core requirements of the POSIX spec, that way your commands (operators, snytax) run on all of them.
Note
Meaning of the name Shell: The word originally meant an outer shell around commands. People also say the outer shell around the kernel.
sh
What is sh?
shstands for shell, more specificallybourne shell.shoriginal shell that shipped with Unix in1977- contrast bash was written 10 years later for GNU to be an OSS version ofshshis both an interactive command interpreter and a scripting language - scripts are just commands saved to a file and run through the same interpreter- you should always write scripts targeted for
shbecause it is the baseline and ensures your scripts will run other systems, rather than relying extra features from other shells
Execution
Shebang
Minimal example of a sh script:
#!/bin/sh
echo "hello world"- The kernel looks for the syntax in the first line (shebang) and decides how to execute the the file
- The shell this spawns only sees this as comment
- shebang:
#!path to interpreter:/bin/sh
If you need your script to run as another interpreter use:
#!/usr/bin/env bash
#!/usr/bin/env rubySource
Source is useful for setting envrionment variables or customizing the current shell
Pithy
sourcejust doesn’tfork()when it runs the commands
sh helloworld # runs in a new instance of sh
source helloworld # uses current shell to execute the commandsArguments
$# # number of arguments supplied
$* # all arguments at once
# check if there's an argument
if [ $# -ne 2 ]; then
exit 1~/.bash_profile or ~/.profile for functions you want available as commands in every interactive shell session.
Control Flow
Comparison Operators
| String | Numeric | True if |
|---|---|---|
| x = y | x -eq y | x is equal to y |
| x != y | x -ne y | x is not equal to y |
| x < y | x -lt y | x is less than y |
| n/a | x -le y | x is less than or equal to y |
| x > y | x -gt y | x is greater than y |
| n/a | x -ge y | x is greater than or equal to y |
| -n x | n/a | x is not null |
| -z x | n/a | x is null |
Evaluation Operators
| Operator | True if |
|---|---|
| -d file | file exists and is a directory |
| -e file | file exists |
| -f file | file exists and is a regular file |
| -r file | User has read permission on file |
| -s file | file exists and is not empty |
| -w file | User has write permission on file |
| file1 -nt file2 | file1 is newer than file2 |
| file1 -ot file2 | file1 is older than file2 |
Loops
for script in rhel.sh sles.sh; do
echo "Running $script"
bash "$script"
done
# read splits on env var IFS (internal field seperator)
# IFS=\n
while read line; do
echo "$((counter++)): $line"
doneExec
Exec command has the following uses:
Replaces the current shell with a command (no new process)
exec /bin/bashRedirects file descriptors (exec < file, exec > file, exec 2>&1)
exec < input.txt
exec > output.txt
exec 2>&1Grep
TODO: grep cheatsheet, explain defaults
grep --ignore-case # -i