in /etc/profile.d/foo.sh I have:
set -o vi
export ECLIPSE_HOME=/usr/local/eclipse
alias eclipse=${ECLIPSE_HOME}/eclipse
After rebooting my Ubuntu 11.10 32-bit install, and after logging in from the GUI and opening a terminal as a regular user, and type alias eclipse the alias is not defined: bash: alias: eclipse: not found
If I then do sudo su - and then alias eclipse then I correctly get alias eclipse='/usr/local/eclipse/eclipse'
Just to be sure, if I then do (as root) su - myusername and then alias eclipse then I correctly get alias eclipse='/usr/local/eclipse/eclipse'
What gives?
It's better to set aliases in bashrc than in profile. profile is only read if you start a login shell. bashrc is read whenever you start an interactive shell.
Related
I'm trying to create an EC2 User-data script to run other scripts on boot up. However, the scripts that I run fail to recognize some commands and variables that I'd already declared. I'm running the commands as the "ubuntu" user but it still isn't working.
My user-data script looks something like this:
export user="ubuntu"
sudo su $user -c ". ./run_script"
Within the script, I have these lines:
THIS_PATH="/some/path"
echo "export SOME_PATH=$THIS_PATH" >> ~/.bashrc
source ~/.bashrc
However, the script can't run SOME_PATH/application, and echo $SOME_PATH this returns a blank line. I'm confused because $SOME_PATH/application works when I log into the EC2 using SSH and my debug logs using whoami returns "ubuntu."
Am I missing something here?
Your data script is executed as root and su command leaves $HOME and other ENV variables intact (note that sudo is redundant). "su -" does not help either
So, do not use ~ or $HOME but full path /home/ubuntu/.bashrc
I found out the problem. It seems that source ~/.bashrc isn't enough to restart the shell -- the environment variables worked after I referenced them in another bash script.
I usually use bash scripts to setup my environments (mostly aliases that interact with Docker), ie:
# ops-setup.sh
#!/bin/bash
PROJECT_NAME="my_awesome_project"
PROJECT_PATH=`pwd`/${BASH_SOURCE[0]}
WEB_CONTAINER=${PROJECT_NAME}"_web_1"
DB_CONTAINER=${PROJECT_NAME}"_db_1"
alias chroot_project="cd $PROJECT_PATH"
alias compose="chroot_project;COMPOSE_PROJECT_NAME=$PROJECT_NAME docker-compose"
alias up="compose up -d"
alias down="compose stop;compose rm -f --all nginx web python"
alias web_exec="docker exec -ti $WEB_CONTAINER"
alias db="docker exec -ti $DB_CONTAINER su - postgres -c 'psql $PROJECT_NAME'"
# ...
I'd like them to be run when I open the embedded terminal.
I tried Startup Tasks but they are not run in my terminal contexts.
Since I have a dedicated script for each of my projects, I can't run them from .bashrc or other.
How can I get my aliases automatically set at terminal opening ?
Today I'm running . ./ops-setup.sh manually each time I open a new embedded terminal.
You can create an alias in your .bashrc file like so:
alias ops-setup='bash --init-file <(echo '. /home/test/ops-setup.sh'; echo '. /home/test/.bashrc')'
If you call ops-setup, it will open up a new bash inside your terminal, and source .bashrc like it normally would, as well as your own script.
The only way I see to completely automate this is to modify the source code of your shell, e.g. bash, and recompile it. The files that are sourced are hardcoded into the source code.
I use bash completion all the time to save typing. However there is an oddity I am unable to figure out how to fix on OSX.
If I install bash-completion using Homebrew (brew install bash-completion) and set it up in .bashrc, the tab key will no longer complete environment variables. Without this installed, environment variable completion works as expected.
For example, I have shortcuts for all my SSH accounts for clients... instead of typing ssh myuser#somecrazydomain.com I can just type ssh $SSHCRAZY which is much easier to remember.
Expected behavior: In the built-in bash in OSX I can type ssh $SSHC and hit tab and it completes to the full command as expected. This is what I want.
Observed behavior: In bash using the homebrew bash-completion additions, hitting tab has no effect for environment variables.
Note: All other extensions added by the bash-completion project are desired (git command completion, etc). I don't want to uninstall it, I just want it to also work with environment variables.
Thanks in advance for any help!
You might consider using an SSH config file (~/.ssh/config) to set up your SSH shortcuts instead of using environment variables. You could put into that file:
Host crazy
HostName somecrazydomain.com
User myuser
Then you can just type ssh crazy.
I guess bash-completion must have defined completion rule for ssh. So try add the -o bashdefault option in your .bashrc. For example, if complete -p ssh output like this:
# complete -p ssh
complete -F _func ssh
#
then you can add this to your .bashrc (or .bash_profile):
complete -F _func -o bashdefault ssh
or
eval "$(complete -p ssh | sed 's/ssh$/-o bashdefault ssh/')"
I'm trying to execute a bootup script (to start a Thin server) on a server using an interface called Virtualmin. I'm able to execute the commands with no problem using bash via PuTTY. I have to use Virtualmin, though, in order to have the commands execute on bootup, and I was having problems that I think were the result of Virtualmin not having my environmental variables available to it. Virtualmin uses Bourne shell, and I'm trying to set GEM_HOME and it's not working.
The error I'm getting is as follows:
/sbin/sh: GEM_HOME=/users/home/dquirk/gems: not found
Here are the commands I'm attempting to send . . . I'm thinking there's something wrong with the notation I'm using to try to set GEM_HOME:
GEM_HOME=/users/home/dquirk/gems
export GEM_HOME
/users/home/dquirk/gems/bin/thin start -c /users/home/dquirk/domains/quirkeweb.net/rails/clee -p 10671 -d -e production -a 127.0.0.1 -P /users/home/dquirk/var/run/thin-10671.pid
Figured it out . . . this is on a shared server, and the Virtualmin interface for adding bootup action commands is in the context of a Bourne shell where the user cannot change any environment variables. I created a bash script that changed the needed variables and executed the Thin start command and then put a bash command to load that script as the bootup action command and that worked.
I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
and add to file 'export PYTHONPATH=/mypythonlib'
then
# env
I can see this line
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
when I use sudo command, cannot find that
$ sudo env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH.
If you use sh try /etc/profile, bash try /etc/bashrc and if you use zsh try /etc/zshenv.
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
You should try sudo -i which will simulate logging in as root and source the ~root/.profile.
As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command and my MacPorts installationw orking correctly.
MacBook-Pro:~ root# cat /var/root/.profile
export MANPATH=/opt/local/share/man:$MANPATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
MacBook-Pro:~ root# which port
/opt/local/bin/port
Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.