Bash#
Bash is a shell commonly used in Linux-based operating systems. Mastering Bash allows you to combine results from Linux utilities and create your own, which is a straightforward way to optimize tasks.
Operators#
Operators available in bash are shown in the table above by groups.
Operator Group |
Operators |
Description |
|---|---|---|
Arithmetic Operators |
|
Perform mathematical calculations. |
Comparison Operators |
|
Compare values. |
Logical Operators |
|
Combine multiple conditions. |
String Operators |
|
Compare and manipulate strings. |
File Test Operators |
|
Test file properties. |
Redirection Operators |
|
Redirect input and output of commands. |
Control Operators |
|
Control execution flow of commands. |
Pipeline Operators |
|
Pass output of one command as input to another. |
Completions#
The Bash offers a competion tool that completes the commands you have typed into the shell. While most of packages add their completions automatically, some do not. Here, we’ll look at how you can add competions to your Bash environment.
Consider the basics of this tool:
Bash completions are provided by a separate package. Installation using apt:
apt install bash-completion.Completions is a collection of bash scripts. The collection for whole system is represented in the
/usr/share/bash-completion/completionsfolder.In order to use the completions, you must
sourcethe corresponding bash scirpt. It is typically done automatically done by the~./.bashrcconfiguration script.
The following cell shows the contents of the bash-completion folder.
ls -l /usr/share/bash-completion/
total 116
-rw-r--r-- 1 root root 77084 Apr 3 2022 bash_completion
drwxr-xr-x 1 root root 32768 Dec 25 11:19 completions
drwxr-xr-x 2 root root 4096 Dec 25 11:19 helpers
Use the complete -p command to view the completions currenly available.
complete -p
For now, there is nothing here. The following cell sources the completions for the git command.
source /usr/share/bash-completion/completions/git
After sourcing the correspoinding script the complete is able to find some details about packages.
complete -p
complete -o bashdefault -o default -o nospace -F __git_wrap__gitk_main gitk
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git
Pressing the <TAB> key while typing the git subcommands will provide the corresponding completions.