I want to be able to run bash in different predefined configurations. for example: When i'm at work i'll do something like:
bash work
And it will load the appropriate environment variables.
The way i see it, the best way of doing it is passing the configuration name to the .bashrc command and than just configure it according to the name.
To do that, i need to pass the name to the .bashrc file.. How do i pass arguments to the .bashrc file from the bash command?
Of course alternatives for doing what i want will be appreciated!
Thank you!
From the man page:
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).
I don't think you can pass arguments to your individual bashrc directly, but you can call a common bashrc from your customized rc files and then either use arguments or global variables.
If you just want to set some environment variables, you can of course just manually call your customised rc file. To make this a little nicer from your command line, you can define a respective alias:
alias load_work='~/.workrc'
or with a new bash
alias load_work='bash --rcfile ~/.workrc'
Related
My original .bashrc script is currently used to run model runs. Now I need to manipulate it to compile a completely new model.
My question is, if I save my original .bashrc, as something such as .bwwbashrc, do I need to manipulate the file in some way so it is able to be read or recognized as the .bashrc when I source it within my scripts?
original sourcing
source /home/tsee/.bashrc
What I think the new sourcing would be.
(after creating .bwwbashrc)
source /home/tsee/.bwwbashrc
Just not sure if I need to save it with a certain extension or edit the executable in some sort of way.
Nope, you can name it whatever you want. Executable bit isn't required either.
If you aren't aware of it, the bash --login option might be of interest to you.
To complement Matt’s correct answer, I’d also point out that you can start a new Bash shell that sources your alternative file instead of .bashrc at start-up.
bash --rcfile .bwwbashrc
From the bash man page:
--rcfile file
Execute commands from file instead of the standard personal initialization
file ~/.bashrc if the shell is interactive (see INVOCATION below).
If you want to replace your current shell (with commands and settings from .bashrc), you can run
exec bash --rcfile .bwwbashrc
I know I can just type
$ vi .bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
However how to use shell script to do it? I prefer to write shell script because I need to configure multiple servers, if type one by one I would take me a long time to go.
Can someone guide me how to do this? Thanks a lot!
EDIT: I just realized that you wanted a script to automate the process of adding environment variables. These commands may work for you:
echo "export JAVA_HOME=/usr/lib/jvm/java-7-oracle" >>~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH" >>~/.bashrc
What this does is append the given text to .bashrc. Instead of copying .bashrc files from server to server, run these commands (you could probably write a script for these) on each server. This preserves the contents of the original rc files on each server, which I find is a better idea than completely overwriting the file.
Original answer
Your .bashrc file is actually written as a shell script. You would place the exact same lines in the shell script, possibly with a hashbang at the beginning of the file. For example:
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
# do java stuff here...
If you were to "do java stuff" in this script, this would work fine. However, if these variables are going to be used outside of the script, you would have to "source" this file. That is what happens with .bashrc. Before the first prompt is given, Bash runs source ~/.bashrc to publish the variables defined in .bashrc.
I (on mac osx) often use
export http_proxy=http://192.168.0.205:1099
to proxy http connection to get a highed download speed. To make things easy, I wrote a shell file named proxy.sh to do this:
#!/bin/sh
export http_proxy=http://192.168.0.205:1099
Before I downlaod, I execute proxy.sh shell command, but I found it did't not come into effect.It lost http_proxy variable in current commnad window(terminal). I must type export command in current terminal,it will come into effect.
So I want to know what's reason for this and a solution? thanks.
Running a shell script "normally" (with proxy.sh for example) results in that running in a sub-process so that it cannot affect the environment of the parent process.
Using . or source will run the shell script in the context of the current shell, so it will be able to affect the environment, using one of the following:
. proxy.sh
source proxy.sh
Another possibility (if you're using bash at least) is to create an alias to do the work for you. You can use something like:
alias faster='export http_proxy=http://192.168.0.205:1099'
so that you can then simply type faster on the command line and it will export that variable (in the context of the current shell).
You could also allow for one-shot settings such as:
alias faster='http_proxy=http://192.168.0.205:1099'
and then use:
faster your_program
which would translate into:
http_proxy=http://192.168.0.205:1099 your_program
That's a bash way to set a variable for just the one invocation of a command.
The export variable will only apply to the script -- if you want it to apply to the shell, you need to use source, and execute the script like so:
. ./proxy.sh
or:
source ./proxy.sh
Note the "." in the first example -- the dot follow by space means the script will apply to the shell.
The reason why your script does not work has been explained by Drakosha & how to make your script work has been explained by Anothony. But with the export in the script you need to source your script each time you open a new terminal. A better solution will be to add the export in .bash_profile or .bashrc
Hope this helps!
When executing a shell script a new shell is launched, the script is executed, and the shell dies. That's why you don't see the variable defined in your shell.
I suggest using an alias for the same purpose.
I have used "alias ruby=ruby1.9.1", so I can execute my ruby with this:
ruby 123.rb
or
ruby1.9.1 123.rb
But in my vim, I use :!ruby and get
/bin/bash: ruby: command not found.
I must use :!ruby1.9.1
How does alias work? Why vim doesn't know it?
When Vim starts a process it makes a system call. It has only inherited the environment variables from your shell if you started it from the shell. But it won't know your bash aliases.
Bash aliases are only a convenience when you enter a command line in the Bash shell. They are expanded by Bash only.
If you want real aliases put symlinks in a private hidden folder, and add that folder to your PATH, or use the alternatives facility.
You can try
:set shellcmdflag+=i
to call bass as "interactive" although that does give an annoying message for every shell command executed.
Aliases (unlike environment variables) are not inherited by subshells. So if you want an alias always available, you need to set it in your .bashrc file, so every instance of the shell will get it on startup
I have MySQL installed (MAMP, Mac OS X) but need to call it by the full path each time I access it from the shell. I created an alias: alias mysql='/Applications/MAMP/Library/Bin/mysql, but this only lasts as long as my terminal/Bash session.
What is an effective way for establishing permanent aliases that will work across users? (I need to be able to execute commands from PHP). Should I set up aliases in the Bash start up script (how is that done?), or is it better to edit the sudoers file? (Could use an example of that as well..)
Thanks--
EDIT- Based on answer:
I just tried creating a ~/.bashrc and wrote the following:
alias mysql='/Applications/MAMP/Library/bin/mysql'
But this doesn't seem to have any effect. Is there a special syntax for this file?
Add the command to your ~/.bashrc file.
To make it available to all users, add it to /etc/profile.
Different shell uses different dot file to store aliases.
For mac, the bash shell uses .bash_profile or .profile
For ubuntu, the bash shell uses.bashrc
If you are using zsh shell and ohmyzsh plugin, the dot file is .zshrc
Traditionally, to add a permanent alias, you need to open the dot file and write alias manually like:
alias hello="echo helloworld"
And remember to source the dot file to have it take effect. To source the dot file on ubuntu's bash, type source .bashrc To make the alias available to all users, write to /etc/profile instead of the dot file. Remember to type source /etc/profile for the new alias to take effect.
If you simply want a temporary alias, you do not need to write to dot file. Simply type that same command (alias hello="echo helloworld) on the terminal.
Note that a temporary alias created through the alias command will disappear once the shell is closed.
If you are looking for a single command to generate aliases without open the text editor, read on.
If you have ruby installed on ubuntu, you can create permanent alias with a single command using aka.
gem install aka2
For example:
aka generate hello="echo helloworld" #will generate a alias hello="echo helloworld"
aka destroy hello #will destroy the alias hello
aka edit hello #will prompt you to edit the alias.
With aka, there is no need to write to the dot file with a text editor. And no need to source the dot file too.
You're going about this the wrong way.
Either add /Applications/MAMP/Library/bin/ to your path, or create a script to invoke MySQL and place it in a bin directory which is already in your path.
On a mac the .bashrc file does not get sourced unless you put
source ~/.bashrc in the /etc/profile or /etc/bashrc.
Just thought I would mention that.