How to write a TUI in BASH
Through my travels I've discovered it's possible to write a fully functional Terminal User Interface in BASH. The object of this guide is to document and teach the concepts in a simple way. To my knowledge they aren't documented anywhere so this is essential.
The benefit of using BASH is the lack of needed dependencies. If the system has BASH available, the program will run. Now there are cases against using BASH and they're most of the time valid. However, there are cases where BASH is the only thing available and that's where this guide comes in.
This guide covers BASH
3.2+which covers pretty much every OS you'll come across. One of the major reasons for covering this version is macOS which will be forever stuck on BASH
3.
To date I have written 3 different programs using this method. The best example of a TUI that covers most vital features is fff which is a Terminal File manager.
The quickest way to determine the current Operating System is the
$OSTYPEvariable. This variable is set at compile time in
bashand typically stores the name of the running kernel or the name of the OS itself.
You can also use the command
unameto identify which OS is running. The
unamecommand is POSIX and should be available everywhere. The output from
unamedoes differ from
$OSTYPEbut there's a vast amount of documented information about it. [1] [2]
get_os() { # Figure out the current operating system to handle some # OS specific behavior. # '$OSTYPE' typically stores the name of the OS kernel. case "$OSTYPE" in linux*) # ... ;;# Mac OS X / macOS. darwin*) # ... ;; openbsd*) # ... ;; # Everything else. *) #... ;; esac
}
$OSTYPEvalues.
The table below was populated by users submitting the value of the
$OSTYPEvariable using the following command. If you're running an OS not mentioned below or the output differs, please open an issue with the correct value.
bash -c "echo $OSTYPE"
| OS |
$OSTYPE| | ----- | ---------- | | Linux with glibc |
linux-gnu| | Linux with musl |
linux-musl| | Cygwin |
cygwin| | Bash on Windows 10 |
linux-gnu| | Msys |
msys| | Mingw64 |
msys| | Mingw32 |
msys| | OpenBSD |
openbsd*| | FreeBSD |
freebsd*| | NetBSD |
netbsd| | macOS |
darwin*| | iOS |
darwin9| | Solaris |
solaris*| | Android (Termux) |
linux-android| | Android |
linux-gnu| | Haiku |
haiku|
This function figures out the terminal window size by moving the cursor to the bottom right corner and then querying the terminal for the cursor's position. As the terminal is made up of cells the bottom right corner is equal to the terminal's size.
get_term_size() { # '\e7': Save the current cursor position. # '\e[9999;9999H': Move the cursor to the bottom right corner. # '\e[6n': Get the cursor position (window size). # '\e8': Restore the cursor to its previous position. IFS='[;' read -sp $'\e7\e[9999;9999H\e[6n\e8' -d R -rs _ LINES COLUMNS }
checkwinsize
Note: This only works in
bash 4+.
When
checkwinsizeis enabled and
bashreceives a command, the
LINESand
COLUMNSvariables are populated with the terminal window size. The
(:;:)snippet works as a pseudo command, populating the variables without running anything external.
get_term_size() { shopt -s checkwinsize; (:;:) }
stty
This function calls
stty sizeto query the terminal for its size. The
sttycommand is POSIX and should be available everywhere which makes it a viable alternative to the pure
bashsolutions.
get_term_size() { # Get terminal size ('stty' is POSIX and always available). # This can't be done reliably across all bash versions in pure bash. read -r LINES COLUMNS <Reacting to window size changes.
Using
trapallows us to capture and react to specific signals sent to the running program. In this case we're trapping theSIGWINCHsignal which is sent to the terminal and the running shell on window resize.We're reacting to the signal by running the above
get_term_size()function. The variables$LINESand$COLUMNSwill be updated with the new terminal size ready to use elsewhere in the program.# Trap the window resize signal (handle window resize events). # See: 'man trap' and 'trap -l' trap 'get_term_size' WINCHEscape Sequences
For the purposes of this resource we won't be using
tput. Thetputcommand has a lot of overhead (10-15 msper invocation) and won't make the program any more portable than sticking to standard VT100 escape sequences. Usingtputalso adds a dependency onncurseswhich defeats the whole purpose of doing this inbash.Hiding and Showing the cursor
See:
# Hiding the cursor. printf '\e[?25l'Showing the cursor.
printf '\e[?25h'
See:
# Disabling line wrapping. printf '\e[?7l'Enabling line wrapping.
printf '\e[?7h'
See:
# Move the cursor to 0,0. printf '\e[H'Move the cursor to line 3, column 10.
printf '\e[3;10H'
Move the cursor to line 5.
printf '\e[5H'
See:
# Using terminal size, move cursor to bottom. printf '\e[%sH' "$LINES"
When using these escape sequences and the cursor hits the edge of the window it stops.
See:
# Move the cursor up a line. printf '\e[A'Move the cursor up 10 lines.
printf '\e[10A'
See:
# Move the cursor down a line. printf '\e[B'Move the cursor down 10 lines.
printf '\e[10B'
See:
# Move the cursor back a column. printf '\e[D'Move the cursor back 10 columns.
printf '\e[10D'
See:
# Move the cursor forward a column. printf '\e[C'Move the cursor forward 10 columns.
printf '\e[10C'
See:
# Clear the screen. printf '\e[2J'Clear the screen and move cursor to (0,0).
This mimics the 'clear' command.
printf '\e[2J\e[H'
This sequence allow you to limit the terminal's vertical scrolling area between two points. This comes in handy when you need to reserve portions of the screen for a top or bottom status-line (you don't want them to scroll).
This sequence also has the side-effect of moving the cursor to the top-left of the boundaries. This means you can use it directly after a screen clear instead of
\e[H(
\e[2J\e[0;10r).
See:
# Limit scrolling from line 0 to line 10. printf '\e[0;10r'Set scrolling margins back to default.
printf '\e[;r'
This is the only non VT100 sequences I'll be covering. This sequence allows you to save and restore the user's terminal screen when running your program. When the user exits the program, their command-line will be restored as it was before running the program.
While this sequence is XTerm specific, it is covered by almost all modern terminal emulators and simply ignored in older ones. There is also DECCRA which may or may not be more widely supported than the XTerm sequence but I haven't done much testing.
# Save the user's terminal screen. printf '\e[?1049h'Restore the user's terminal screen.
printf '\e[?1049l'