There is no simple way to obtain syntax highlighting in GNU Bash (or GNU Readline), but it is in principle possible to implement your own line editor in Bash script by binding all the user inputs to shell functions using the builtin command bind -x 'BYTE: SHELL-COMMAND'
. It is of course possible to integrate the feature of syntax highlighting in your own line editor.
In fact, I implemented a line editor ble.sh
with features like syntax highlighting and auto-suggestions. It supports Bash 3.0..5.1. Since it is written in (almost-)pure Bash scripts, you can just source
the script in ~/.bashrc
. Here is an example to set up ble.sh
in the bashrc (see README for details):
$ git clone https://github.com/akinomyoga/ble.sh.git
$ cd ble.sh
$ make
$ make INSDIR="$HOME/.local/share/blesh" install
# bashrc
# Add the following line at the beginning of bashrc
[[ $- == *i* ]] &&
source "$HOME/.local/share/blesh/ble.sh" --attach=none
# ... other bashrc settings ...
# Add the following line at the end of bashrc
[[ ${BLE_VERSION-} ]] && ble-attach
Note: I know that sometimes answering questions with links to own products is considered self-promotion and unpreferable, so I have been refraining from answering this question. However, as no other solutions were offered after a long time despite many views (which reflect significant demand). So I decided to answer this question today. Referring to the following meta-questions/answers, I described the idea first and next provided a link to my project as an example implementation.
Yes, I have to admit that this is actually self-promotion, but I believe that this helps people who want the feature. If there are problems, I would appreciate it if you could tell me that in the comments.
Update 2022-01-12 Update the supported Bash versions. Correct grammar.