Bash

String Manipulation

${str:position}         # substring starting at position
${str:position:len}     # substring starting at position with length len
${str#ubstring}         # delete shortest match from front
${str##substring}       # delete longest match from front
${str%substring}        # delete shortest match from back
${str%%substring}       # delete longest match from back
${str/pattern/replacement}  # pattern replace
${str/#pattern/replacement} # pattern replace at front
${str/%pattern/replacement} # pattern replace at end
${str//pattern/replacement} # global pattern replace
${str^^}                # convert to UPPER-CASE
${str,,}                # convert to lower-case

Parameter Substituion

${str-default}             # set 'default' if not set
${str:-default}            # set 'default' if not set (even if declared null)

${str=default}             # set 'default' if not set
${str:=default}            # set 'default' if not set (even if declared null)

${str+value}               # set 'value' if $str is set, otherwise set null
${str:+value}              # set 'value' if $str is set (even if declared null), otherwise set null

${str?error}               # abort with 'error' if not set
${str:?error}              # abort with 'error' if not set (and not null)

Variables

Arrays

Indexed arrays require no declaration

Check below under “Hashes” for accessing the different properties of an array.

Hashes

Since Bash v4

Here Documents

Bash allow here documents like this

To disable substitution in the here doc text quote the marker with single or double quotes.

To strip leading tabs use

Debugging Scripts

For simple tracing add a

in the script or append the “-x” to the shebang or run the script like this

As “set -x” enables tracing you can disable it with “set +x” again. This allows tracing only a part of the code (e.g. a condition in an inner loop). Additionally to “-x” you may want to set “-v” to see the shell commands that are executed. Combine both to

Writing Safer Scripts

It is a good practice to use

which ensures

  • that you check all commands that can fail

  • that you check for failing commands within pipes

  • that you do not use undefined variables

Network Connections

File Operators

The complete list of bash 4.2 test operators:

String Operators

The complete list of bash 4.2 string operators:

Simulate Reading From a File

Sometimes you might need to pass a file name when you want to pipe output from a commands. Then you could write to a file first and then used it, but you can also use the “>()” or “<()” operator. This can be used with all tools that demand a file name paramter:

History

History Handling

Here are some improvements for the bash history handling:

Adding Timestamps

To add timestamps to your history set the following environment variable:

Easier History Navigation

If you do not like Ctrl-R to nagivate the history you can define other keys as PgUp and PgDown in /etc/inputrc:

History Hardening

For a secure bash configuration add the following settings to your global/users bashrc

and finally mark the history file append only

Misc

Waiting for child processes

Via https://spin.atomicobject.com/2017/08/24/start-stop-bash-background-process/

Kill childs on exit

Command Completion

How to setup your own bash completion schemas. Here is a git example:

Note that the above example propably already comes prepared with your Linux distribution. You might want to check default definitions installed in /etc/bash_completion.d for a good starting point.

Kill all childs on exit

Apply ulimit Changes Instantly

The problem behind this is documented in this blog postarrow-up-right but it boils down to try to use the “-i” switch:

If it doesn’t work you might need to investigate and change the PAM configuration.

PS1: Escape Non-Print Chars

To avoid incorrect line break behaviour when editing the command line you need to escape control characters in PS1 like this:

For example:

Last updated