Update .bashrc from provisioning shell script with Vagrant - linux

I'm trying to add some additional lines to .bashrc in my home directory from the provisioning shell script when launching a new instance with Vagrant.
In the shell script I have:
set -x
sudo apt-get update
sudo apt-get install vim
echo "source /usr/local/share/chruby/chruby.sh">> ~/.bashrc
echo "source /usr/local/share/chruby/auto.sh">> ~/.bashrc
However after completion nothing has been written to .bashrc.
This is a cut down version of the full script the intention of which is to install Ruby/Rails.

You need to give the full path to the file.
E.g.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc

Add these lines to .bashrc
if [ -f /usr/local/share/chruby/chruby.sh ]; then
. /usr/local/share/chruby/chruby.sh
fi
It will textually include the script into .bashrc and execute it when opening a new shell.

Try this for your last 2 lines - it should give you exactly what you need.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
echo "source /usr/local/share/chruby/auto.sh" >> /home/vagrant/.bashrc

Related

In Linux, when does the "bash" command source a ~/.bash_profile or ~/.profile file?

I am currently using ssh to access a linux computer. I use the command:
ssh -t user#hostaddress 'cd ~/Desktop && bash'
When I get there, I see that neither ~/.bash_profile nor ~/.profile are sourced. What are the rules surrounding when these are sourced in? The reason I call bash is because I am able to get terminal colors when I do bash (blue folders, etc) that I otherwise cannot get just by using ssh user#hostaddress.
You're not running bash as a login shell -- using bash -l should source .bash_profile. Otherwise you can use .bashrc.

centos7 “export: command not found

I have followed a tutorial to install JDK 8 on my computer Centos7 OS, but in the last part I tried to set environment varibles.
In the last part of the tutorial I typed:
echo “export JAVA_HOME=/opt/jdk1.8.0_101” > /etc/profile.d/jre.sh
echo “export JRE_HOME=/opt/jdk1.8.0_101/jre” >> /etc/profile.d/jre.sh
echo “export PATH=$PATH:/opt/jdk1.8.0_101/bin:/opt/jdk1.8.0_101/jre/bin” >> /etc/profile.d/jre.sh
and then I am seeing this when I open the terminal:
bash: $'\342\200\234export':order not found
bash: $'\342\200\234export': order not found
bash: $'\342\200\234export': order not found
[evconsul8#localhost ~]$
Path:
[evconsul8#localhost ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/evconsul8/.local/bin:/home/evconsul8/bin
After that I Trying to search the cause open
~/.bash_profile
[root#localhost evconsul8]# gedit ~/.bash_profile
Result:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export JAVA_HOME=/opt/jdk1.8.0_101
export JRE_HOME=/opt/jdk1.8.0_101/jre
export PATH=$PATH:/opt/jdk1.8.0_101/bin:/opt/jdk1.8.0_101/jre/bin
export PATH
Note: In one of my attemps after the error I added manually the lines corresponding to JAVA_HOME, JRE_HOME and PATH=$PATH in the file above. It was ok??
The principal problem I guess it is causing troubles to other apps in my machine.
You should use regular double quotes (") to enclose a string and not the fancy ones (“) you are using.

linux: how to execute profile file [duplicate]

This question already has answers here:
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 3 years ago.
My colleague gave me a file containing lots of configs such as
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
How can I use(execute) this profile?
You can load the profile using source command:
source <profile-filename>
eg:
source ~/.bash_profile
For your custom aliases, the best place should be at ~/.bash_aliases. You must just be sure the ~/.bashrc file already contains the following lines:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
or in a more concise manner:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
To load them immediately, source it. Otherwise, aliases will be loaded at every terminal opening. To check, use the alias command without argument.
You can put it in your local .bashrc (or appropriate shell rc file) file if you want it permanently.
cat fileNameProfile >> ~/.bashrc
And for current session:
source ~/.bashrc
If you want it just now, then:
source fileNameProfile
For one time use, copy paste commands to your terminal:-
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
For everytime use, add it in .bashrc.
echo 'alias ll="ls -l"' >> ~/.bashrc
echo 'alias lr="ls -lrt"' >> ~/.bashrc
echo 'alias gv="vim -g"' >> ~/.bashrc
and then source ~/.bashrc
To autoload these alias commands, create a .sample_profile file and add the alias on the file.
after that add this text in .bashrc file
.bashrc
if [ -f ~/.sample_profile ]; then
. ~/.sample_profile
fi

Cannot add environment variables in linux

For an entire day I have been tryng to add an environment variable to linux ad it isn't working. This is the guide that I am following. This is what is written in the .profile file.
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
JAVA_HOME=/usr/local/java/jdk1.8.0_51
JRE_HOME=$JAVA_HOME/jre
LARAVEL=/home/user/.composer/vendor/bin
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$LARAVEL
export JAVA_HOME
export JRE_HOME
export LARAVEL
export PATH
I am running the file. But when I run echo $PATH I am not getting the $LARAVEL path. What am I doing wrong?
Have you logged out and logged in? Also try to update your .bashrc file instead. I think that .profile is only used for the ksh shell.
Try to source the file, like;
$ source yourfilename
In Unix/Linux (unlike in Windows) a program cannot affect the environment of the parent process. When you are running it as a regular shellscript it becomes a subprocess of the parent, and it will affect only it's own environment and not that of the parent.
Using the keyword source (or using the dot .) will instruct your process to executed the shell script directly rather than fork a new subprocess.
echo 'export PATH="$PATH":/path/to/folder/' >> ~/.bashrc
and
source ~/.bashrc

Node.js stops working when I log out

I logged in as root to my CentOS 5/cPanel server and I typed the following:
cd /usr/local/bin/
git clone --depth 1 http://github.com/joyent/node.git
cd node
git checkout origin/v0.4 # optional. Note that master is unstable.
export JOBS=2 # optional, sets number of parallel commands.
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
source ~/.profile
It seems to be working fine until I log out from the server and log back in it's as it wasn't installed:
[~]# node test.js
-bash: node: command not found
If I type: source ~/.profile it starts working again until I log out.
Please help. Thanks.
EDIT:
This is the content of my .bash_profile, how should I change it?
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Maybe you have a .bash_profile and it's being used instead?

Resources