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: a here-string <<< feeds a variable to a command inside the shell

Shell
~/tips/tip-shell-here-string-stdin

IFS=@ read -r user host <<< "jordi@nas"

Gives a command a string as input without a pipe or a temporary file.

Piping echo into a command just to give it text is something I avoid.

IFS=@ read -r user host <<< "jordi@nas"
grep -c error <<< "$log_text"

<<< passes a string as standard input and adds a trailing newline. read splits on whitespace by default, so IFS=@ in front of it splits on the at sign for that one command. Unlike echo "$x" | read a b, read runs in the current shell, so the variables stay set. It is a bash, zsh and ksh feature and not part of POSIX sh. For several lines, use a here-document with <<'EOF'.

Further reading: Bash manual: Here Strings

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link