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 two multi-line strings which have any single-character delimiter to separate columns or 2+ spaces.
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.
The -s
argument is optional and can be used to set the box style. Default is 1.
The -p
argument is optional and can be passed in to alter the padding left and right of the data. Default is 1.
The -d
argument is optional and can be passed in to specify the data delimiter if it is not a tab or 2+ spaces.
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.
Also included is a script that will read in stdin and call the display-box function dynamically. This is useful for piping in your content directly. 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 ,
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 two example scripts included in the examples
directory. The examples/example-all.sh
script will display the example in all available styles.
There is also an inline example in examples/example-inline.sh
which shows how to pipe in your content.
- 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 margin 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 |