Bash will source automatic profiles such as .bashrc. --rcfile option can be used to override the automatic script. But I need to source additional personalized file (that's the automatic script plus another file) when launching the bash shell without touching ANY files in $HOME or /etc directory since $HOME directory belongs to application run user. The personalized file must not be located in $HOME directory.
Is this possible?
I tried:
/bin/bash <<EOF
. /a-directory-outside-of-home/vanilla
EOF
but it returned to the current shell.
Okay, so you want to run the user's normal .bashrc, followed by your own script, and you want to trigger this behavior in the way that bash is called, correct?
The call:
/bin/bash --rcfile myscript
First line of myscript:
source $HOME/.bashrc
bash --rcfile <(cat rcfile1; cat rcfile2)
works just fine and requires no modifications anywhere.
Add your stuff as *.sh files in /etc/profile.d -- read /etc/profile and
http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
I wouldn't modify the .bashrc file. Instead, I would modify the .bash_profile
file which is located at $HOME.
It is the place from where .bashrc is included.
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/bashrc
fi
A good option for you is to add a .personalized_settings file and include it
just below the above lines like this.
# Adding personalized settings
if [ -f ~/.personalized_settings ]; then
. ~/personalized_settings
fi
This requires that .personalized_settings file is located at $HOME.
I have tested the above in Fedora12.
Edit :
You might need to look for .profile instead of .bash_profile in Ubuntu (and hopefully other Debian based systems). (courtesy #Benjamin W.)
This (link)[https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files] will give you more information on it.
What if you mess around with any of the profile files?
You have a back-up copy of all these files in /etc/skel which you could
use to restore.
Related
When I open the .bashrc file using gedit, or vi editor, there's nothing in it. I am on MINT trying to install Hadoop and one of the step requires editing the .bashrc file, to save the environment variable. I remember doing the same on Ubuntu, and there were contents were in it.
Can you help me get them all back? Is it possible to get them all back?
When I do gedit ~/.bashrc it opens to a blank page.
Thanks in advance!
From looking at the MINT forums, it doesn't look like MINT supplies a default .bashrc:
http://forums.linuxmint.com/viewtopic.php?f=90&t=130358
However, .bashrc is an optional file, so you can add whatever you want to it. Check out the bash man pages:
man bash
Also, if you setup a .bashrc file, make sure it is getting called from either .profile or .bash_profile like this:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You should have one in here:: /etc/skel/.bashrc
Try copying it to your home folder:: cp /etc/skel/.bashrc ~/.bashrc
Look for .bashrc.swap files in your home directory by doing so:
ls -Fa ~ | grep '*bashrc*'
I don't know how gedit or other programs name their swap files, but just look what comes up.
If there are no such files, you're out of luck.
I installed a text editor under /usr/share/my-editor/editor-executable.
When I want to launch the editor in terminal I have to write the complete path:
# /usr/share/my-editor/editor-executable file-to-open
I would create a command for the editor so I can simply digit:
# my-editor file-to-open
How can I do?
The reason you couldn't launch your executable is because the shell look for the command in any of the paths defined in the PATH environment variable (known paths from now).
You can check those known paths with:
echo $PATH
As you can see, /usr/bin is defined there as well as other paths.
Anyway, you can get what you want in several ways.
Note below that when I use the ~ directory, the command will be only available for the current user.
Creating an alias my-editor
This is my favourite when you want to run a command which is not found in the known paths. It would be a good idea for you too. In bash you can place the alias in ~/.bash_aliases.
echo alias my-editor=/usr/share/my-editor/my-editor-executable >> ~/.bash_aliases
Creating a link to your file in some of the known paths
It's the way you have done it and just to clarify, if you had created the link in any of the known paths, it would have worked too.
ln -s /usr/share/my-editor/my-editor-executable /usr/bin/my-editor
Defining a function with name my-editor
I think it's too much due to your needs but it's up to you if want to give it a try. It can be useful for other purposes.
You must define it in a file read by your shell. e.g. ~/.bashrc in bash. Shell files invocation.
cat >> ~/.bashrc << "EOF"
function my-editor() {
/usr/share/my-editor/my-editor-executable "$#"
}
EOF
Adding /usr/share/my-editor/ to the PATH
You can add a new path to the PATH variable. In Ubuntu, the PATH variable is generally set in /etc/environment and if you modify this file, the new path will be accesible for all users.
However, if you want to be the only one who has access to the new path, you can set it in one of the personal shell files. e.g. in bash: ~/.bashrc. Shell files invocation.
echo 'export PATH="$PATH:/usr/share/my-editor/"' >> ~/.bashrc
[bash] Entering a command into the hash table
A singular way to get the same result in bash is adding my-editor into the shell hash table. Again, you must add the command in some file read by bash (~/.bashrc).
echo 'hash -p /usr/share/my-editor/my-editor-executable my-editor' >> ~/.bashrc
Moving the executable to a known path
Finally, if you don't need the file (my-editor-executable) in his current directory anymore, you can simply move it to a known path.
mv /usr/share/my-editor/my-editor-executable /usr/bin/my-editor
I answer by myself:
I created a link to executable file under /usr/bin :
# ln -sF /usr/share/my-editor/my-editor-executable /usr/bin/my-editor
Now it is possible to run the application "my-editor" via terminal everywhere in the file system
I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help
When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command
If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.
Lets say I have a perl script that setups a bash_aliases as follow
source file: bash_aliases.sh
alias w="cd /var/www"
install.pl
#!/usr/bin/perl
my $aliase_src= "${installation_dir}/bash_aliases.sh";
my $aliase_target="$ENV{'HOME'}/.bash_aliases";
if (! copy($aliase_src,$aliase_target))
{
print "Failed to copy file $aliase_src to Destination:$aliase_target. Reason:$! .";
die;
}
[command required to load .bash_aliases file]
I should be able to go as follow:
./install.pl
w
I dont want to :
./install.pl
source ~/.bash_aliases
w
I am unable to find command to load these aliases such that once the file is moved to home directory these aliases should be available to current script (install.pl) as well as all other script without running another command from command line e.g source ~/.bash_aliases OR . .bash_aliases
Edit
auto_load_aliases.sh using PROMPT_COMMAND (trying to following l0b0, answer)
#!/bin/bash
echo 'alias test="echo tested"' > $HOME/.bash_aliases
export PROMPT_COMMAND="$HOME/.bash_aliases";
this also did not work :
./auto_load_aliases.sh
test
What I expected what that auto_load_aliases.sh script sets the PROMPT_COMMAND , once it's set I have test aliase available.But that did not work.
~/.bash_aliases is not loaded by Bash by default, it is just a common convenience to avoid putting too much stuff in ~/.bashrc. If you want the aliases to be available in new Bash shells you have to source ~/.bash_aliases somewhere in ~/.bashrc. Some popular distributions (I believe Ubuntu is one) does this by default.
If you want to automatically source ~/.bash_aliases after changing it, you could try adding this in ~/.bashrc to source it after each command:
PROMPT_COMMAND='source ~/.bash_aliases'
If your alias file is very complex, you could make this faster by using inotifywait and only reloading the file when it changes.
Another way of loading aliases after changing them would be to replace the current shell with another one:
exec "$SHELL"
Bottom line, something has to change to automatically load new aliases. There is no built-in runtime configuration option in Bash to do this (and even then you'd have to enable that option).
After start the cygwin shell, it just locate in a wrong home dir:
xfire#codingme.com ~
$ pwd
/cygdrive/c/Users/xfire
But it used to be /home/xfire
xfire#codingme.com /etc
$ cat passwd | grep xfire
xfire:unused:22773:10513:U-CORP\xfire,S-1-5-21-527237240-725345543-682003330-12773:/home/xfire:/bin/bash
And the .bashrc in the /home/xfire was not executed, even I copy it to the /cygdrive/c/Users/xfire, it also doesn't work!
On cygwin, I add this to my ~/.bash_profile:
. ~/.bashrc
Some program add an "HOME" environment in windows registry and set the value to "C:\Users\xfire", that's why cygwin take that directory as the home. cygwin.com/faq-nochunks.html
On my version of cygwin I found that only ~/.profile was being execueted so added
if [ -e "${HOME}/.bash_profile" ]; then
source "${HOME}/.bash_profile"
fi
to the .profile file. My .bash_profile file contains another test for .bashrc and executes that from inside there. I also added the following two lines to my .bashrc file.
export BASH_ENV="${HOME}/.profile"
export ENV="${HOME}/.profile"
The first of these ensures that .profile gets executed in non-interactive terminals and the second ensures it gets executed in POSIX terminals. I found a very useful explanation of what get run and when in the Bash Reference Manual.
In your case it wouldn't help as you have an issue with the value of your HOME environment variable but this page comes up quite high on the list when searching for this issue.
You can also also set BASH_ENV variable, e.g., BASH_ENV='C:\DOCUME~1\dwyttenb\.bashrc'
~/.bash_profile is executed before the initial command prompt is returned to the user. After that, every time a new shell is opened, ~/.bashrc is executed.
Adding . ~/.bashrc at end of ~/.bash_profile has resolved the problem.
Now you can check existing aliases using alias command.
Find more details on github.