What’s the shell?
- Multiple interfaces exist: GUIs, voice interfaces, AR/VR.
- Graphical interfaces are limited.
- For full control, use a textual interface: The Shell.
- Platforms offer a variety of shells.
- Introduction to “bash” (Bourne Again SHell).
Accessing the Shell
- To start: you need a terminal.
- A shell prompt is where you type commands.
# missing:$ ··································· bash
Using the shell
- Typing a command at the prompt will execute it.
- Arguments can be passed to commands.
# missing:$ ··································· bash
dateMore Shell Commands
- Commands are parsed by whitespace.
$PATHhelps find executable programs.
# missing:$ ··································· bash
echo hello
# we expect helloNavigating in the shell
- Understanding paths.
pwdshows the current directory.cdchanges directories.- Use
lsto list contents.
Shell Permissions
# missing:$ ··································· bash
ls -l ~- Using
mv,cp,mkdir. - The
mancommand for manuals.
Connecting programs
- Programs have input and output streams.
- Redirection with
< fileand> file. - Using pipes
|to chain commands.
More about Programs and Pipes
# missing:$ ··································· bash
ls -l / | tail -n1- Use of
curl,grep, andcut.
A versatile and powerful tool
- Understanding the “root” user.
- The use of
sudofor superuser actions. - Kernel parameters in
sysfs.
Working with sysfs
- Adjusting laptop screen brightness.
- Permission issues.
- Using
teeto overcome permissions.
A small break…
Job Control
Interrupting a Job
- Need to stop lengthy commands, like a
findcommand in large directories. - Common way:
Ctrl-C. - How and why does this work?
Killing a process
- Shell communicates via signals.
- Signals act as software interrupts.
Ctrl-Csends aSIGINTsignal to the process.
A Python script that captures SIGINT and ignores it.
#!/usr/bin/env python
import signal, time
def handler(signum, time):
print("\nI got a SIGINT, but I am not stopping")
signal.signal(signal.SIGINT, handler)
i = 0
while True:
time.sleep(.1)
print("\r{}".format(i), end="")
i += 1- Sending
SIGINTto above program will not stop it. SIGQUIT(by typingCtrl-\) can be used instead.
$ python sigint.py
24^C
I got a SIGINT, but I am not stopping
26^C
I got a SIGINT, but I am not stopping
30^\[1] 39913 quit python sigint.py
Additional Signals
SIGINTandSIGQUIT: Terminal related.SIGTERM: Generic signal for graceful exit.- Use
kill -TERM <PID>to sendSIGTERM.
Pausing and Backgrounding Processes
SIGSTOPpauses a process.Ctrl-ZsendsSIGTSTP, a terminal version ofSIGSTOP.- Continue with
fg(foreground) orbg(background). - Check jobs with
jobscommand.
Example Session
$ sleep 1000
^Z
[1] + 18653 suspended sleep 1000
$ nohup sleep 2000 &
...
$ jobs
...
$ kill -STOP %1
...
$ kill -SIGHUP %1
...
Signals to Remember
SIGKILL: Can’t be captured, always terminates the process.- More signals: https://en.wikipedia.org/wiki/Signal_(IPC)
- Check signals with
man signalorkill -l.
Terminal Multiplexers
Introduction
- Want multiple terminal sessions? Use terminal multiplexers.
- Examples:
tmuxandscreen. - Run side-by-side sessions, detach and reattach later.
Using tmux
- Keybindings form:
<C-b> x. - Sessions, Windows, and Panes.
- Detailed commands and hierarchy explained.
Aliases
Introduction
- Shortcuts for long commands or common flags.
- Structure:
alias alias_name="command_to_alias arg1 arg2"
Alias Examples
alias ll="ls -lh"
alias gs="git status"
alias v="vim"
alias sl=ls
alias mv="mv -i"
...Aliases Persistence
- Aliases do not persist by default.
- Add to startup files like
.bashrcor.zshrc.
Dotfiles
Introduction
- Plain-text configuration files.
- Examples:
.vimrc,.bashrc. - Shell,
git,vim,ssh, andtmuxare configured using dotfiles.
Organizing Dotfiles
- Keep in a separate folder.
- Use version control.
- Symlink them using a script.
Benefits of Organizing Dotfiles
- Easy installation on new machines.
- Portability across systems.
- Synchronization between machines.
Further Reading
- Detailed resources for shell startup scripts,
tmux, andscreen. - Explore and customize your toolset!
Portability
A common pain with dotfiles is the configurations might not work across machines. Especially if they have different operating systems or shells.
Sometimes configurations should only apply to a specific machine. There are tricks to make this easier:
- Use if-statements for machine-specific customizations.
- Use includes for machine-specific settings.
- Share configurations across programs.
if [[ "$(uname)" == "Linux" ]]; then {do_something}; fi
if [[ "$SHELL" == "zsh" ]]; then {do_something}; fi
if [[ "$(hostname)" == "myServer" ]]; then {do_something}; fiIn a ~/.gitconfig:
[include]
path = ~/.gitconfig_local
And ~/.gitconfig_local can have machine-specific settings.
For sharing configurations, such as aliases:
if [ -f ~/.aliases ]; then
source ~/.aliases
fiShells & Frameworks
While bash is popular, there are alternatives like zsh with enhanced features:
- Smarter globbing
- Spelling correction
- Better tab completion
Frameworks improve shells:
Terminal Emulators
Choosing a terminal emulator is vital. Consider:
- Font
- Color Scheme
- Shortcuts
- Tabs/Panes
- Scrollback
🆕 Warp. For Windows users: the new Terminal