When called with the -n <dbnum> option and an invalid db index, the cli fails to select that database (as expected). However, instead of displaying the error, the invalid index is displayed while index 0 is effectively in use.

Demonstrated on unstable:

$ redis-cli config get databases
1) "databases"
2) "16"
$ redis-cli info keyspace
# Keyspace
$ redis-cli set db0 ""
OK
$ redis-cli -n 16 set db16 ""
OK
$ redis-cli -n 16 
127.0.0.1:6379[16]> info keyspace
# Keyspace
db0:keys=2,expires=0,avg_ttl=0
127.0.0.1:6379[16]> select 16
(error) ERR invalid DB index

Comment From: anishk25

I can take on the issue. I am new to the project and this seems like beginner friendly bug.

Comment From: HeartSaVioR

Yeah this was also reported last year (#1812) and I submitted pull request (#2283). I'd like to see this resolved regardless whose PR would be accepted, since it's over than 1 year and a half.

Comment From: huangzhw

Fixed in https://github.com/redis/redis/pull/8898