Bash function or script to display nice-looking boxes around your content like:
The function will automatically determine the length of each column and draw the box appropriately. It accepts one or two multi-line strings which have any single-character delimiter or 2+ spaces to separate columns.
Required:
The first contains the header data with tabs separating out the columns.
The second contains the body data which is typically a multi-line string with tabs separating the columns.
Note: You can combine these into a single string if you prefer. Just separate them by one or more blank lines.
Optional:
The -s
argument can be used to set the box style. Default is 1.
The -p
argument can be passed in to alter the padding left and right of the column data. Default is 1.
The -d
argument can be passed in to specify the data delimiter if it is not a tab or 2+ spaces.
The -m
argument can be passed in to add a margin (empty lines) above and below the display box. Default is 0.
It is recommended you use the argument prefixes to call these, but they are optional for backwards compatibility. The old syntax of: display-box "$headers" "$body" 4 2
is still valid.
The display-box.sh script can be sourced and called as a function, or a simplified option is the inline script option. Just pipe input to the script. It does still allowing you to pass in arguments to modify the settings.
The syntax of this inline example is:
echo "$multi_line_string" | /path/to/display-box.sh -s 2 -p 2 -d , -m 1
To make it more convenient, you may want to create a symlink in your path, such as: /usr/local/bin/display-box
or perhaps ~/bin/display-box
if you don't have sudo access.
sudo ln -s /path/to/display-box.sh /usr/local/bin/display-box
Then you can just pipe any string to display-box
. Example:
echo "
ID,First Name,Last Name,Email
1,Jimmy,Smith,[email protected]
2,Sarah,Jane,[email protected]
3,Mark,Jones,[email protected]
" | display-box -d ,
Deeper dive walk-through of each section:
Include the function
Create two strings that are tab-separated
Optionally define the box style and the padding
Call the display-box function passing in the required $headers and $body variables. Other optional variables are Box Style and Padding
There are several different box styles you can choose between
There are several example scripts included in the examples
directory.
example.sh
shows a comma-separated column delimiter inline example.example-all-styles.sh
displays the example in all available styles.example-tabs.sh
shows an example of sourcing the script to make thedisplay-box
function available for repeated calls. It uses the default tab delimiter.example-whitespace.sh
shows how you can maintain a data format that mirrors the display-box columns using only spaces.
- Add Colors (borders, background, separate for header/body)
- Option to add drop-shadow
- Separate padding-left from padding-right
- Possibly add different padding for header vs body
- Option to add bars between each line in body (like Excel)
- Add style for solid block bars (https://en.wikipedia.org/wiki/Block_Elements)
- Add style with double-bars for header and single bars for body
- Add style with double-bars for header and outside but single bars for body columns
- Add style with only horizontal bars
- Option to add space between columns (as opposed to padding) for styles without vertical bars
Version Number | Date Released | Description of changes |
---|---|---|
v1.0 | 30 Apr 2020 | Initial version |
v1.0.1 | 22 Mar 2022 | Minor bug fix |
v1.1 | 13 May 2023 | Added custom delimiter and inline functionality |
v1.2 | 30 May 2024 | Added margin and trimmed input text for new lines |