Matt Elhotiby
Matt Elhotiby

Reputation: 44066

What is wrong with this shell command

I need to add this to .bashrc

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile

but i am using a shell script and i need to run this under another user so i tried the following..

BASH_COMMAND='[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"'
sudo su ${USER} -c "echo '${BASH_COMMAND}' >> ${HOME_BASE}${USER}/.bashrc"

I was expecting to see

'[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

in my .bashrc

but i see this instead

sudo su ${USER} -c "echo echo 'sudo su ${USER} -c "echo '${BASH_COMMAND}' >> ${HOME_BASE}${USER}/.bashrc"' >> /home/deploy/.bashrc >> ${HOME_BASE}${USER}/.bashrc"

What am i doing wrong with the script

Upvotes: 1

Views: 85

Answers (1)

mpapis
mpapis

Reputation: 53158

shell can sometimes misinterpret redirection, you can overcome this with:

RVM_COMMAND='[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"'
echo "${RVM_COMMAND}" | sudo su ${USER} -c 'tee -a "$HOME/.bashrc"' >/dev/null

Upvotes: 1

Related Questions