11

I want to append several lines of shell commands to a file owned by root. I have sudo access. In short I want to put this:

export M2_HOME=/opt/apache-maven-3.1.1 
export M2=$M2_HOME/bin 
PATH=$M2:$PATH 

I tried this:

m2config=$(cat << EOL
export M2_HOME=/opt/apache-maven-3.1.1
export M2=\$M2_HOME/bin
PATH=\$M2:\$PATH
EOL
)

and then

sudo bash -c "echo $m2config >> /etc/profile.d/maven.sh"

But to no avail. Does anyone know how to do this? I have consulted many similar questions but none addressing this exact need.

1 Answer 1

29
sudo bash -c "cat >> /etc/profile.d/maven.sh" << EOL
export M2_HOME=/opt/apache-maven-3.1.1
export M2=\$M2_HOME/bin
PATH=\$M2:\$PATH
EOL

If you don't fancy spawning a subshell, sudo tee -a /etc/profile.d/maven.sh > /dev/null << EOL works just as well.

Sign up to request clarification or add additional context in comments.

1 Comment

note that the shell usually tries to expand variables ($M2). You can prevent this by specifying 'EOL' instead of EOL in the first line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.