If on Linux, with bash versions prior to 5.1 (or zsh), you could do:
{
chmod u+w /dev/fd/3 # only needed in bash 5.0
rbenv local > /dev/fd/3
IFS= read -rd '' -u 3 variable
} 3<<< ''
That does use a temp file like every here-document or here-string, though that's hidden to you.
bash 5.1 switched to using pipes instead of regular temp files (at least when the contents of the herefile/herestring is small enough to fit in a pipe buffer). With those, you can always create the temp file by hand:
tmpfile=$(mktemp) || exit
{
rm -f -- "$tmpfile"
rbenv local >&3
IFS= read -rd '' -u4 variable
} 3> "$tmpfile" 4< "$tmpfile"
(deleting the tmpfile early like <</<<< do to minimise the risk of it being left lying around if the script is killed or rbenv exits the shell).
If rbenv outputs less data than can fit in a pipe without blocking (typically 64KiB), still on Linux and Linux only, you can use a pipe instead of the temp file with:
{
rbenv local > /dev/fd/3
IFS= read -rd '' -u 3 variable
} 3< <(:)
With ksh93 or recent versions of mksh, use the form of command substitution that doesn't start a subshell:
variable=${
rbenv local
}
That one is also currently available in development versions of bash and zsh.
Beware that contrary to the IFS= read -rd '' approach, that removes the trailing newline characters in the output¹ (like in regular command substitution).
To achieve that, internally, current (as of 2024) versions of ksh93 use a pre-deleted tempfile (in /dev/shm on my Debian system), mksh as well (in /tmp on my system), zsh-dev same except the file is deleted later and bash-dev an anonymous memfd tempfile where supported and a pre-deleted tempfile in /tmp where not.
In older versions of zsh, you leverage the =(...) form of process substitution to get an automatically deleted temp file (here passed as argument to an anonymous function):
() { rbenv local > $1; IFS= read -rd '' variable <$1; } =()
¹ though in the case of zsh, whether to do that or not or only when ${ cmd } is left unquoted is currently being debated.
<fifo" ! – Yunus Jan 03 '17 at 14:17$(...)or temporary file? – Kusalananda Jan 03 '17 at 14:24rbenv localchanges some variables and I want to use these variables. The shell script will run on various projects and I can't trust in/tmpor permissions. Some machines I just can write on/var/tmp. – dgmike Jan 03 '17 at 14:28.ruby-versionfile in the current directory? BTW, I can not find anything that saysrbenv localchanges anything. It's supposed to only report the local version according to https://github.com/rbenv/rbenv#rbenv-local – Kusalananda Jan 03 '17 at 14:36rvm(the shell script will prevent therbenvandrvm) some variables are set, likeRUBY_VERSION. But I think I can usecat .ruby-version. :-) – dgmike Jan 03 '17 at 14:57$TMP,$TMPDIRor$TEMPset in your environment? These are the environment variables that Ruby itself checks when finding the temp folder. – JigglyNaga Jan 03 '17 at 16:04tmpfile. It must exist another way. – dgmike Jan 03 '17 at 18:35$( )method? Is a temp subshell that will only be used to return to your variable the output of the command.... – George Vasiliou Mar 29 '17 at 14:59read variable < <(rbenv local)witch as far as i know works on the same shell using process substitution. – George Vasiliou Mar 29 '17 at 15:01