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 scripts 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.0. Since it is written in (almost-)pure Bash scripts, you can just
source the script in
~/.bashrc. Here is a sample to setup
ble.sh in bashrc (see README for details):
$ git clone https://github.com/akinomyoga/ble.sh.git
$ cd ble.sh
$ make INSDIR="$HOME/.local/share/blesh" install
# 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_bash)) && 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 to this question. However, no other solutions did not appear long time, and also this question has many views (which reflects its significant demand). So I decided to answer to this question today. Referring 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 by comments.