brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: redis-cli -x takes the last argument of a command from stdin

CLI
~/tips/tip-redis-cli-x-read-value-stdin

redis-cli -x SET notes < notes.txt

Reads the final argument of a Redis command from standard input, so a whole file can become a value.

Putting a multi-line file into a Redis value with a quoted argument on the command line is a quoting headache. redis-cli has a flag that skips all of it.

redis-cli -x SET notes < notes.txt

With -x, redis-cli reads the last argument of the command from stdin. The Redis docs use it to store /etc/services in a key and read part of it back with GETRANGE, and call it useful for scripting.

The value is stored exactly as it arrives. In a throwaway instance on redis-cli 7.0.15, a file containing a b and c on two lines came back as "a b\nc\n", spaces and newlines included.

That is also the caveat. A command like echo hi | redis-cli -x SET greeting stores hi plus the newline that echo adds: STRLEN greeting returned 3 there. Use printf %s hi or echo -n if you don’t want the newline.

The help text also lists a sibling, -X, which takes a tag name and puts the stdin data where that tag appears in the command. The example given there copies a key to another name:

redis-cli -D "" --raw dump key > key.dump
redis-cli -X dump_tag restore key2 0 dump_tag replace < key.dump

-D "" sets an empty delimiter between replies, as the usage text describes for raw formatting. That sequence is straight from the usage examples. A run in a scratch instance with a small string key restored it as key2 without problems.

Further reading: Redis CLI: get input from other programs

More CLI tips wsl --shutdown wsl --distribution <Distribution Name> --user <User Name> Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft.zip wsl --terminate <Distribution Name> rg -o -r '$2' '"(GET|POST) (\S+)' access.log All CLI tips (60) All topics

Link