1

I am trying to append eval "$(rbenv init -)" to my bash profile

(I am trying to follow this instruction)

# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

This is part of an automated process so I don't have access to a GUI or can't use an editor. but when I echo the command $(rbenv init -) gets executed and append bunch of stuff. How can I echo it as plain text?

this is what I have now

ec2-user@ip-172-31-46-129 ~ % cat /tmp/install-rbenv.sh 
+-zsh:41> cat /tmp/install-rbenv.sh
#!/bin/bash
sudo -i -u buildkite-agent bash << EOF
echo "export RUBY_CONFIGURE_OPTS=\"--with-openssl-dir=$(brew --prefix openssl@1.1)\"" >> /Users/buildkite-agent/.bash_profile
echo 'eval "$(rbenv init -)"' >> /Users/buildkite-agent/.bash_profile
EOF
echo "Done"

running it

ec2-user@ip-172-31-46-129 ~ % . /tmp/install-rbenv.sh 
+-zsh:42> . /tmp/install-rbenv.sh
+/tmp/install-rbenv.sh:6> brew --prefix openssl@1.1
+/tmp/install-rbenv.sh:6> rbenv init -
+/tmp/install-rbenv.sh:6> sudo -i -u buildkite-agent bash
+/tmp/install-rbenv.sh:10> echo Done
Done

checking the bash profile

ec2-user@ip-172-31-46-129 ~ % sudo su - buildkite-agent 
+-zsh:43> sudo su - buildkite-agent
rbenv: no such command `sh-'
-bash: eval: line 31: syntax error near unexpected token `rehash'
-bash: eval: line 31: `  rehash|shell)'

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
ip-172-31-46-129:~ buildkite-agent$ cat ~/.bash_profile 
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/opt/openssl@1.1"
eval "export PATH="/Users/ec2-user/.rbenv/shims:${PATH}"
export RBENV_SHELL=zsh
source /usr/local/Cellar/rbenv/1.1.2/libexec/../completions/rbenv.zsh
command rbenv rehash 2>/dev/null
rbenv() {
  local command
  command="${1:-}"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  rehash|shell)
    eval "$(rbenv "sh-$command" "$@")";;
  *)
    command rbenv "$command" "$@";;
  esac
}"
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62

2 Answers2

2

Apparently you don't want to use an editor to edit your .bash_profile file or you are afraid you will got trapped inside vi :-D

Put the text you want to append to the file in apostrophes and it will go verbatim to the file. The shell does not do any expansion in the arguments that are wrapped in apostrophes.

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

That's all.

axiac
  • 68,258
  • 9
  • 99
  • 134
1

Use apostrophes around the first EOF, i.e. 'EOF' instead of EOF. bash distinguishes between the two and won't evaluate stuff if you use the apostrophe version.

> bash << EOF > test.sh
echo '$(date)'
EOF
> cat test.sh
Mon 12 Apr 2021 08:08:53 PM CEST
> bash << 'EOF' > test.sh
echo '$(date)'
EOF
> cat test.sh
$(date)
Czaporka
  • 2,190
  • 3
  • 10
  • 23