Bash Script
Common Bash Use Cases
TOC:
Common Bash Use Cases
Table of Contents
STRINGS
Trim leading and trailing white-space from string
Trim all white-space from string and truncate spaces
Use regex on a string
Split a string on a delimiter
Change a string to lowercase
Change a string to uppercase
Reverse a string case
Trim quotes from a string
Strip all instances of pattern from string
Strip first occurrence of pattern from string
Strip pattern from start of string
Strip pattern from end of string
Percent-encode a string
Decode a percent-encoded string
Check if string contains a sub-string
Check if string starts with sub-string
Check if string ends with sub-string
ARRAYS
Reverse an array
Remove duplicate array elements
Random array element
Cycle through an array
Toggle between two values
LOOPS
Loop over a range of numbers
Loop over a variable range of numbers
Loop over an array
Loop over an array with an index
Loop over the contents of a file
Loop over files and directories
FILE HANDLING
Read a file to a string
Read a file to an array (by line)
Get the first N lines of a file
Get the last N lines of a file
Get the number of lines in a file
Count files or directories in directory
Create an empty file
Extract lines between two markers
FILE PATHS
Get the directory name of a file path
Get the base-name of a file path
VARIABLES
Assign and access a variable using a variable
Name a variable based on another variable
ESCAPE SEQUENCES
Text Colors
Text Attributes
Cursor Movement
Erasing Text
PARAMETER EXPANSION
Indirection
Replacement
Length
Expansion
Case Modification
Default Value
BRACE EXPANSION
Ranges
String Lists
CONDITIONAL EXPRESSIONS
File Conditionals
File Comparisons
Variable Conditionals
Variable Comparisons
ARITHMETIC OPERATORS
Assignment
Arithmetic
Bitwise
Logical
Miscellaneous
ARITHMETIC
Simpler syntax to set variables
Ternary Tests
TRAPS
Do something on script exit
Ignore terminal interrupt (CTRL+C, SIGINT)
React to window resize
Do something before every command
Do something when a shell function or a sourced file finishes executing
PERFORMANCE
Disable Unicode
OBSOLETE SYNTAX
Shebang
Command Substitution
Function Declaration
INTERNAL VARIABLES
Get the location to the
bash
binaryGet the version of the current running
bash
processOpen the user's preferred text editor
Get the name of the current function
Get the host-name of the system
Get the architecture of the Operating System
Get the name of the Operating System / Kernel
Get the current working directory
Get the number of seconds the script has been running
Get a pseudorandom integer
INFORMATION ABOUT THE TERMINAL
Get the terminal size in lines and columns (from a script)
Get the terminal size in pixels
Get the current cursor position
CONVERSION
Convert a hex color to RGB
Convert an RGB color to hex
CODE GOLF
Shorter
for
loop syntaxShorter infinite loops
Shorter function declaration
Shorter
if
syntaxSimpler
case
statement to set variable
OTHER
Use
read
as an alternative to thesleep
commandCheck if a program is in the user's PATH
Get the current date using
strftime
Get the username of the current user
Generate a UUID V4
Progress bars
Get the list of functions in a script
Bypass shell aliases
Bypass shell functions
Run a command in the background
Capture the return value of a function without command substitution
AFTERWORD
\
STRINGS
Trim leading and trailing white-space from string
This is an alternative to sed
, awk
, perl
and other tools. The function below works by finding all leading and trailing white-space and removing it from the start and end of the string. The :
built-in is used in place of a temporary variable.
Example Function:
Example Usage:
Trim all white-space from string and truncate spaces
This is an alternative to sed
, awk
, perl
and other tools. The function below works by abusing word splitting to create a new string without leading/trailing white-space and with truncated spaces.
Example Function:
Example Usage:
Use regex on a string
The result of bash
's regex matching can be used to replace sed
for a large number of use-cases.
CAVEAT: This is one of the few platform dependent bash
features. bash
will use whatever regex engine is installed on the user's system. Stick to POSIX regex features if aiming for compatibility.
CAVEAT: This example only prints the first matching group. When using multiple capture groups some modification is needed.
Example Function:
Example Usage:
Example Usage in script:
Split a string on a delimiter
CAVEAT: Requires bash
4+
This is an alternative to cut
, awk
and other tools.
Example Function:
Example Usage:
Change a string to lowercase
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Change a string to uppercase
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Reverse a string case
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Trim quotes from a string
Example Function:
Example Usage:
Strip all instances of pattern from string
Example Function:
Example Usage:
Strip first occurrence of pattern from string
Example Function:
Example Usage:
Strip pattern from start of string
Example Function:
Example Usage:
Strip pattern from end of string
Example Function:
Example Usage:
Percent-encode a string
Example Function:
Example Usage:
Decode a percent-encoded string
Example Function:
Example Usage:
Check if string contains a sub-string
Using a test:
Using a case statement:
Check if string starts with sub-string
Check if string ends with sub-string
ARRAYS
Reverse an array
Enabling extdebug
allows access to the BASH_ARGV
array which stores the current function’s arguments in reverse.
CAVEAT: Requires shopt -s compat44
in bash
5.0+.
Example Function:
Example Usage:
Remove duplicate array elements
Create a temporary associative array. When setting associative array values and a duplicate assignment occurs, bash overwrites the key. This allows us to effectively remove array duplicates.
CAVEAT: Requires bash
4+
CAVEAT: List order may not stay the same.
Example Function:
Example Usage:
Random array element
Example Function:
Example Usage:
Cycle through an array
Each time the printf
is called, the next array element is printed. When the print hits the last array element it starts from the first element again.
Toggle between two values
This works the same as above, this is just a different use case.
LOOPS
Loop over a range of numbers
Alternative to seq
.
Loop over a variable range of numbers
Alternative to seq
.
Loop over an array
Loop over an array with an index
Loop over the contents of a file
Loop over files and directories
Don’t use ls
.
FILE HANDLING
CAVEAT: bash
does not handle binary data properly in versions < 4.4
.
Read a file to a string
Alternative to the cat
command.
Read a file to an array (by line)
Alternative to the cat
command.
Get the first N lines of a file
Alternative to the head
command.
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Get the last N lines of a file
Alternative to the tail
command.
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Get the number of lines in a file
Alternative to wc -l
.
Example Function (bash 4):
Example Function (bash 3):
This method uses less memory than the mapfile
method and works in bash
3 but it is slower for bigger files.
Example Usage:
Count files or directories in directory
This works by passing the output of the glob to the function and then counting the number of arguments.
Example Function:
Example Usage:
Create an empty file
Alternative to touch
.
Extract lines between two markers
Example Function:
Example Usage:
FILE PATHS
Get the directory name of a file path
Alternative to the dirname
command.
Example Function:
Example Usage:
Get the base-name of a file path
Alternative to the basename
command.
Example Function:
Example Usage:
VARIABLES
Assign and access a variable using a variable
Alternatively, on bash
4.3+:
Name a variable based on another variable
ESCAPE SEQUENCES
Contrary to popular belief, there is no issue in utilizing raw escape sequences. Using tput
abstracts the same ANSI sequences as if printed manually. Worse still, tput
is not actually portable. There are a number of tput
variants each with different commands and syntaxes (try tput setaf 3
on a FreeBSD system). Raw sequences are fine.
Text Colors
NOTE: Sequences requiring RGB values only work in True-Color Terminal Emulators.
Sequence | What does it do? | Value |
---|---|---|
| Set text foreground color. |
|
| Set text background color. |
|
| Set text foreground color to RGB color. |
|
| Set text background color to RGB color. |
|
Text Attributes
NOTE: Prepend 2 to any code below to turn it's effect off (examples: 21=bold text off, 22=faint text off, 23=italic text off).
Sequence | What does it do? |
---|---|
| Reset text formatting and colors. |
| Bold text. |
| Faint text. |
| Italic text. |
| Underline text. |
| Blinking text. |
| Highlighted text. |
| Hidden text. |
| Strike-through text. |
Cursor Movement
Sequence | What does it do? | Value |
---|---|---|
| Move cursor to absolute position. |
|
| Move cursor to home position ( | |
| Move cursor up N lines. |
|
| Move cursor down N lines. |
|
| Move cursor right N columns. |
|
| Move cursor left N columns. |
|
| Save cursor position. | |
| Restore cursor position. |
Erasing Text
Sequence | What does it do? |
---|---|
| Erase from cursor position to end of line. |
| Erase from cursor position to start of line. |
| Erase the entire current line. |
| Erase from the current line to the bottom of the screen. |
| Erase from the current line to the top of the screen. |
| Clear the screen. |
| Clear the screen and move cursor to |
PARAMETER EXPANSION
Indirection
Parameter | What does it do? |
---|---|
| Access a variable based on the value of |
| Expand to |
| Expand to |
Replacement
Parameter | What does it do? |
---|---|
| Remove shortest match of pattern from start of string. |
`${VAR | |
--- |
PATTERN}` | Remove longest match of pattern from start of string. |
| ${VAR%PATTERN}
| Remove shortest match of pattern from end of string. | | ${VAR%%PATTERN}
| Remove longest match of pattern from end of string. | | ${VAR/PATTERN/REPLACE}
| Replace first match with string. | ${VAR//PATTERN/REPLACE}
| Replace all matches with string. | ${VAR/PATTERN}
| Remove first match. | ${VAR//PATTERN}
| Remove all matches.
Length
Parameter | What does it do? |
---|---|
| Length of var in characters. |
| Length of array in elements. |
Expansion
Parameter | What does it do? |
---|---|
| Remove first |
| Get substring from |
| Get first |
| Remove last |
| Get last |
| Cut first |
Case Modification
Parameter | What does it do? | CAVEAT |
---|---|---|
| Uppercase first character. |
|
| Uppercase all characters. |
|
| Lowercase first character. |
|
| Lowercase all characters. |
|
| Reverse case of first character. |
|
| Reverse case of all characters. |
|
Default Value
Parameter | What does it do? |
---|---|
| If |
| If |
| If |
| If |
| If |
| If |
| Display an error if empty or unset. |
| Display an error if unset. |
BRACE EXPANSION
Ranges
String Lists
CONDITIONAL EXPRESSIONS
File Conditionals
Expression | Value | What does it do? |
---|---|---|
|
| If file exists. |
|
| If file exists and is a block special file. |
|
| If file exists and is a character special file. |
|
| If file exists and is a directory. |
|
| If file exists. |
|
| If file exists and is a regular file. |
|
| If file exists and its set-group-id bit is set. |
|
| If file exists and is a symbolic link. |
|
| If file exists and its sticky-bit is set |
|
| If file exists and is a named pipe (FIFO). |
|
| If file exists and is readable. |
|
| If file exists and its size is greater than zero. |
|
| If file descriptor is open and refers to a terminal. |
|
| If file exists and its set-user-id bit is set. |
|
| If file exists and is writable. |
|
| If file exists and is executable. |
|
| If file exists and is owned by the effective group ID. |
|
| If file exists and is a symbolic link. |
|
| If file exists and has been modified since last read. |
|
| If file exists and is owned by the effective user ID. |
|
| If file exists and is a socket. |
File Comparisons
Expression | What does it do? |
---|---|
| If both files refer to the same inode and device numbers. |
| If |
| If |
Variable Conditionals
Expression | Value | What does it do? |
---|---|---|
|
| If shell option is enabled. |
|
| If variable has a value assigned. |
|
| If variable is a name reference. |
|
| If the length of string is zero. |
|
| If the length of string is non-zero. |
Variable Comparisons
Expression | What does it do? |
---|---|
| Equal to. |
| Equal to (synonym for |
| Not equal to. |
| Less than (in ASCII alphabetical order.) |
| Greater than (in ASCII alphabetical order.) |
ARITHMETIC OPERATORS
Assignment
Operators | What does it do? |
---|---|
| Initialize or change the value of a variable. |
Arithmetic
Operators | What does it do? |
---|---|
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Exponentiation |
| Modulo |
| Plus-Equal (Increment a variable.) |
| Minus-Equal (Decrement a variable.) |
| Times-Equal (Multiply a variable.) |
| Slash-Equal (Divide a variable.) |
| Mod-Equal (Remainder of dividing a variable.) |
Bitwise
Operators | What does it do? |
---|---|
| Bitwise Left Shift |
| Left-Shift-Equal |
| Bitwise Right Shift |
| Right-Shift-Equal |
| Bitwise AND |
| Bitwise AND-Equal |
| Bitwise OR |
| Bitwise OR-Equal |
| Bitwise NOT |
| Bitwise XOR |
| Bitwise XOR-Equal |
Logical
Operators | What does it do? |
---|---|
| NOT |
| AND |
| OR |
Miscellaneous
Operators | What does it do? | Example |
---|---|---|
| Comma Separator |
|
ARITHMETIC
Simpler syntax to set variables
Ternary Tests
TRAPS
Traps allow a script to execute code on various signals. In pxltrm (a pixel art editor written in bash) traps are used to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit.
Traps should be added near the start of scripts so any early errors are also caught.
NOTE: For a full list of signals, see trap -l
.
Do something on script exit
Ignore terminal interrupt (CTRL+C, SIGINT)
React to window resize
Do something before every command
Do something when a shell function or a sourced file finishes executing
PERFORMANCE
Disable Unicode
If unicode is not required, it can be disabled for a performance increase. Results may vary however there have been noticeable improvements in neofetch and other programs.
OBSOLETE SYNTAX
Shebang
Use #!/usr/bin/env bash
instead of #!/bin/bash
.
The former searches the user's
PATH
to find thebash
binary.The latter assumes it is always installed to
/bin/
which can cause issues.
NOTE: There are times when one may have a good reason for using #!/bin/bash
or another direct path to the binary.
Command Substitution
Use $()
instead of ` `
.
Function Declaration
Do not use the function
keyword, it reduces compatibility with older versions of bash
.
INTERNAL VARIABLES
Get the location to the bash
binary
bash
binaryGet the version of the current running bash
process
bash
processOpen the user's preferred text editor
Get the name of the current function
Get the host-name of the system
Get the architecture of the Operating System
Get the name of the Operating System / Kernel
This can be used to add conditional support for different Operating Systems without needing to call uname
.
Get the current working directory
This is an alternative to the pwd
built-in.
Get the number of seconds the script has been running
Get a pseudorandom integer
Each time $RANDOM
is used, a different integer between 0
and 32767
is returned. This variable should not be used for anything related to security (this includes encryption keys etc).
INFORMATION ABOUT THE TERMINAL
Get the terminal size in lines and columns (from a script)
This is handy when writing scripts in pure bash and stty
/tput
can’t be called.
Example Function:
Example Usage:
Get the terminal size in pixels
CAVEAT: This does not work in some terminal emulators.
Example Function:
Example Usage:
Get the current cursor position
This is useful when creating a TUI in pure bash.
Example Function:
Example Usage:
CONVERSION
Convert a hex color to RGB
Example Function:
Example Usage:
Convert an RGB color to hex
Example Function:
Example Usage:
CODE GOLF
Shorter for
loop syntax
for
loop syntaxShorter infinite loops
Shorter function declaration
Shorter if
syntax
if
syntaxSimpler case
statement to set variable
case
statement to set variableThe :
built-in can be used to avoid repeating variable=
in a case statement. The $_
variable stores the last argument of the last command. :
always succeeds so it can be used to store the variable value.
OTHER
Use read
as an alternative to the sleep
command
read
as an alternative to the sleep
commandSurprisingly, sleep
is an external command and not a bash
built-in.
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
For performance-critical situations, where it is not economic to open and close an excessive number of file descriptors, the allocation of a file descriptor may be done only once for all invocations of read
:
(See the generic original implementation at https://blog.dhampir.no/content/sleeping-without-a-subprocess-in-bash-and-how-to-sleep-forever)
Check if a program is in the user's PATH
Get the current date using strftime
strftime
Bash’s printf
has a built-in method of getting the date which can be used in place of the date
command.
CAVEAT: Requires bash
4+
Example Function:
Example Usage:
Get the username of the current user
CAVEAT: Requires bash
4.4+
Generate a UUID V4
CAVEAT: The generated value is not cryptographically secure.
Example Function:
Example Usage:
Progress bars
This is a simple way of drawing progress bars without needing a for loop in the function itself.
Example Function:
Example Usage:
Get the list of functions in a script
Bypass shell aliases
Bypass shell functions
Run a command in the background
This will run the given command and keep it running, even after the terminal or SSH connection is terminated. All output is ignored.
Capture the return value of a function without command substitution
CAVEAT: Requires bash
4+
This uses local namerefs to avoid using var=$(some_func)
style command substitution for function output capture.
Last updated