Contents
Bash scripting automates tasks using commands and control structures. It supports variables, strings, and special variables for script execution and return codes. Bash also provides tools for file searching, code finding, and command recall.
Table of Contents
- Basics of Bash Scripting
- Variables and Strings in Bash
- Special Variables in Bash
- Command Execution and Return Codes
- Permissions – Brief Recap
- Default permissions
- Script Example
- Command Substitution Using
$( CMD )
- Control Flow and Functions
- Shell Functions vs. Scripts
- Bash Comparisons and Globbing
- More Shell tools
- Discovering Command Usage
- Finding Files
- Finding Code
- Finding Shell Commands
- Directory Navigation
Basics of Bash Scripting
Bash scripting allows you to automate tasks by writing sequences of commands in a file, known as a script.
- Execution of Commands: Scripts can run any command you can type in the terminal.
- Control Flow Expressions: Utilize
if
,for
,while
, and other control structures to make decisions. - Tailored for Shell Tasks: Ideal for automating command-line tasks, file manipulation, and system administration.
Variables and Strings in Bash
In Bash scripting, you can assign values to variables and use them throughout your script.
-
Assigning Variables: Use the syntax
variable_name=value
without spaces around the=
. -
Accessing Variables: Prefix the variable name with
$
to access its value. -
Quoting Strings:
- Double Quotes (
"
): Variables inside double quotes are expanded (their values are used). - Single Quotes (
'
)}: Variables inside single quotes are treated as literal strings (no expansion).
Example:
- Double Quotes (
Special Variables in Bash
Bash provides several special variables that give you information about your script and the environment:
-
$0
: The name of the script itself. -
$1
to$9
: The first nine arguments passed to the script.$1
is the first argument,$2
is the second, and so on. -
$@
: All the arguments passed to the script as an array. -
$#
: The number of arguments passed to the script. -
$?
: The return code (exit status) of the last executed command. A value of0
typically means success. -
$$
: The process ID (PID) of the current script. -
!!
: The entire last command executed in the shell, including arguments. Useful for repeating commands, especially withsudo
: -
$_
: The last argument of the previous command. In interactive shells, you can also retrieve this by pressingEsc
followed by.
orAlt + .
.
TIP
As noted,
$9
is the highest numbered positional parameter. To access more than nine arguments, use${10}
,${11}
, and so on until${ARG_MAX}
For a comprehensive list of special characters and variables in Bash, refer to Special Characters in Bash.
Command Execution and Return Codes
Understanding how commands execute and return codes is essential in Bash scripting.
- Conditional Execution:
&&
: Executes the next command only if the previous command succeeded (returned exit status0
).||
: Executes the next command only if the previous command failed (non-zero exit status).;
: Executes commands sequentially, regardless of the exit status of previous commands.
true
andfalse
Commands:true
: A command that does nothing and returns a success exit status (0
).false
: A command that does nothing and returns a failure exit status (1
).
Example:
Permissions – Brief Recap
Unix-like operating systems use a permission system to control access to files and directories.
Permission Types:
-
Read (r):
- Files: Allows viewing the contents of a file.
- Directories: Allows listing the contents within the directory.
-
Write (w):
- Files: Allows modifying or deleting the file’s contents.
- Directories: Allows adding, removing, or renaming files within the directory.
-
Execute (x):
- Files: Allows executing the file as a program or script.
- Directories: Allows accessing or traversing into the directory.
User Categories:
- User (u): The owner of the file or directory.
- Group (g): Users who are part of the file’s group.
- Other (o): All other users not covered by user or group.
Permissions are often represented in the format rwxrwxrwx
, where each set of rwx
corresponds to user, group, and other.
TIP
r = 4
,w = 2
,x = 1
in case you need to set permissions numerically.
Default permissions
When a new file is created in Unix, it typically has the following default permissions:
- User (Owner): Read and Write (
rw-
) - Group: Read-only (
r--
) - Other: Read-only (
r--
)
This is depicted as:
rw-r--r--
These defaults can be modified using the umask
command, which sets default permission masks.
Script Example
Below is an example of a Bash script that demonstrates variable usage, command substitution, and control flow.
NOTE
It’s recommended to use
[[ ]]
for conditional expressions as they are more robust than[ ]
.
Command Substitution Using $( CMD )
Command substitution allows you to capture the output of a command and use it within another command.
Example in a Dockerfile:
GNUPGHOME="$(mktemp -d)"
: Creates a temporary directory and assigns it toGNUPGHOME
.gpg --recv-keys "$key"
: Imports a GPG key.- Command substitution is used to set environment variables or capture command outputs within scripts.
Control Flow and Functions
Bash supports typical control flow structures:
- Conditional Statements:
if
,else
,elif
- Case Statements:
case
for matching patterns - Loops:
while
: Repeats as long as a condition is true.for
: Iterates over a list of items.
Function Example: Creating and Entering a Directory
You can define functions to encapsulate reusable code.
Usage:
This command will create my_new_directory
and then change into it.
Shell Functions vs. Scripts
Understanding the difference between functions and scripts is important:
- Functions:
- Written in the shell’s language.
- Loaded into memory once when the shell starts or when the function is defined.
- Can modify the shell’s environment (e.g., change directories, set variables).
- Promote code modularity, reuse, and clarity.
- Typically included in your shell configuration files like
.bashrc
.
- Scripts:
- It can be written in any scripting language (Bash, Python, Perl, etc.).
- Executed as separate processes each time they are run.
- Cannot modify the parent shell’s environment directly (changes are limited to the script’s process).
- Useful for automating tasks and can be invoked from any shell.
In summary,
- use functions for quick, shell-specific tasks that may need to interact with the shell environment.
- use scripts for more complex tasks or when using different languages.
Bash Comparisons and Globbing
Comparisons:
-
Use
[[ ]]
for conditional expressions in Bash. It’s more powerful and safer than[ ]
. Examples:
Globbing:
-
Wildcards are used to match file names and patterns.
*
: Matches any number of any characters (including none).?
: Matches exactly one character.[ ]
: Matches any one character inside the brackets.{}
: Brace expansion for generating arbitrary strings.
Examples:
Globbing is a powerful feature for working with multiple files and automating tasks.
More Shell tools
Discovering Command Usage
When working with the shell, you might encounter unfamiliar commands or need to recall how to use a command.
Resources:
-
--help
: Most commands provide a quick help message. -
man
Command: Access the manual pages for detailed information. -
tldr
Pages: Simplified and community-driven man pages.Install
tldr
with:
These tools help you quickly understand command usage and options.
Finding Files
Searching for files in the file system is a common task.
Tools:
-
find
:-
Searches for files in a directory hierarchy.
-
Syntax:
-
Example:
-
-
fd
:-
A simpler and faster alternative to
find
. -
Syntax:
-
Example:
-
-
locate
:-
Searches a database of files for quick results.
-
Requires updating the database periodically with
updatedb
. -
Example:
-
Usage Scenarios:
- Use
find
when you need complex search criteria or actions. - Use
fd
for simpler and faster searches. - Use
locate
when you need quick results and the database is up-to-date.
Finding Code
Searching within files for specific patterns is essential for code and text analysis.
Tools:
grep
:-
Searches for patterns in files.
-
Syntax:
-
- Alternatives:
ack
: Designed for programmers, faster thangrep
.ag
(The Silver Searcher): Very fast code searching tool.rg
(Ripgrep): Extremely fast search tool, respects.gitignore
by default.
Pattern Searching Examples:
-
Search recursively for a pattern in all
.py
files: -
Using
rg
for the same task:
These tools help you find code snippets, functions, or any text within files quickly.
Finding Shell Commands
Recall previous commands or search through your command history.
Tools and Techniques:
-
history
Command: -
Reverse Search with
Ctrl + R
:- Press
Ctrl + R
and start typing to search backward through your history.
- Press
-
fzf
(Fuzzy Finder):-
An interactive command-line fuzzy finder.
-
Can be integrated with shell history for faster searching.
-
Installation:
-
Usage:
-
Autosuggestions:
- Fish Shell or Zsh with plugins can provide command autosuggestions based on history.
Protecting Sensitive Entries:
- Be cautious with sensitive data in commands (like passwords).
- Avoid typing sensitive information directly; use input prompts or configuration files.
Directory Navigation
Advanced tools can make navigating directories more efficient.
Tools:
-
tree
:-
Displays directories and files in a tree-like format.
-
Usage:
-
-
broot
:-
A modern, interactive tree command with fuzzy search.
-
Installation:
-
-
nnn
:- A terminal-based file browser with minimalistic design.
- Supports plugins for extended functionality.
-
ranger
:- A console file manager with VI key bindings.
- Provides a preview of the selected file.
These tools enhance your ability to navigate and manage the file system from the command line.
NOTE
We will not cover regular expressions , but they are a powerful tool for pattern matching and text processing in the shell. For more practice with regular expressions, visit RegexOne.