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.
Using the shell
- Typing a command at the prompt will execute it.
- Arguments can be passed to commands.
More Shell Commands
- Commands are parsed by whitespace.
$PATH
helps find executable programs.
Navigating in the shell
- Understanding paths.
pwd
shows the current directory.cd
changes directories.- Use
ls
to list contents.
Shell Permissions
- Using
mv
,cp
,mkdir
. - The
man
command for manuals.
Connecting programs
- Programs have input and output streams.
- Redirection with
< file
and> file
. - Using pipes
|
to chain commands.
More about Programs and Pipes
- Use of
curl
,grep
, andcut
.
A versatile and powerful tool
- Understanding the “root” user.
- The use of
sudo
for superuser actions. - Kernel parameters in
sysfs
.
A small break…
Job Control
Interrupting a Job
- Need to stop lengthy commands, like a
find
command 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-C
sends aSIGINT
signal to the process.
A Python script that captures SIGINT
and ignores it.
- Sending
SIGINT
to 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
SIGINT
andSIGQUIT
: Terminal related.SIGTERM
: Generic signal for graceful exit.- Use
kill -TERM <PID>
to sendSIGTERM
.
Pausing and Backgrounding Processes
SIGSTOP
pauses a process.Ctrl-Z
sendsSIGTSTP
, a terminal version ofSIGSTOP
.- Continue with
fg
(foreground) orbg
(background). - Check jobs with
jobs
command.
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 signal
orkill -l
.
Terminal Multiplexers
Introduction
- Want multiple terminal sessions? Use terminal multiplexers.
- Examples:
tmux
andscreen
. - 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
Aliases Persistence
- Aliases do not persist by default.
- Add to startup files like
.bashrc
or.zshrc
.
Dotfiles
Introduction
- Plain-text configuration files.
- Examples:
.vimrc
,.bashrc
. - Shell,
git
,vim
,ssh
, andtmux
are 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.
In a ~/.gitconfig
:
[include]
path = ~/.gitconfig_local
And ~/.gitconfig_local
can have machine-specific settings.
For sharing configurations, such as aliases:
A small break…
Remote Machines
Remote servers are becoming more common in a programmer’s workflow. SSH is the tool to know.
To SSH into a server:
SSH can also run commands directly:
SSH Keys
Key-based authentication uses public-key cryptography. The private key (~/.ssh/id_rsa
or ~/.ssh/id_ed25519
) is your password.
Key Generation
Use ssh-keygen
to generate a key pair:
Choose a passphrase for security.
Key Based Authentication
SSH checks .ssh/authorized_keys
. To copy a public key:
Or use ssh-copy-id
:
Copying Files Over SSH
- Use
ssh+tee
for simple copies. - Use
scp
for bulk copying. - Use
rsync
for efficient copying.
Port Forwarding
Link local ports to remote ones. Two types:
- Local Port Forwarding
- Remote Port Forwarding
Local Port Forwarding
Remote Port Forwarding
Example of local port forwarding:
Navigate to localhost:9999
on your local machine.
SSH Configuration
Instead of using shell aliases:
Use ~/.ssh/config
:
Miscellaneous
Consider Mosh for roaming connections and sshfs to mount remote folders.
Shells & 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