I’m trying to build a script that automates the sourcing of multiple
bash alias files using ruby. I’m running into problems in that it while
my alias files get run, the terminal session doesn’t recognize any of
the aliases that were set.
I’ll use a simple example to be more clear:
My original .bash_rc originally looked like this:
. ~/.bash_aliases
My .bash_aliases was a simple test:
alias rt=“echo the rt alias worked”
echo the alias file was run
When I source directly with
. ~/.bash_rc
The alias gets set, I see the message ‘the alias file was run’ and the
rt command works as expected. So, every bash terminal session has
access to rt. Great. Awesome. That’s how it should work.
However, I want to set aliases using Ruby. I change my .bash_rc
from: . ~/.bash_aliases
to: ruby ~/set_aliases.rb
My set_aliases.rb looks like this:
system “source ~/.bash_aliases”
Now, each terminal session I open actually runs the file. I know this
because I get the message ‘the alias file was run’. However, the
terminal session doesn’t have access to the rt alias (command unknown).
I’m thinking that the problem has to do with the process context that
the aliases are set in, but I don’t know how to fix the problem.
I’m thinking that the problem has to do with the process context that
the aliases are set in, but I don’t know how to fix the problem.
Ding. When you call system(), it fires up a subordinate shell which
runs whatever command you pass in. Then, that shell exits and your
aliases are gone.
What’s the problem you’re trying to solve? I think there’s probably a
better (non-Ruby) way to do what you want.
I’m trying to build a script that automates the sourcing of multiple
bash alias files using ruby. I’m running into problems in that it while
my alias files get run, the terminal session doesn’t recognize any of
the aliases that were set.
…
I’m thinking that the problem has to do with the process context that
the aliases are set in, but I don’t know how to fix the problem.
Any ideas would be greatly appreciated. Thanks.
You’re right. The aliases are getting added to the environment created
by ‘system’, but not added back to the parent. I’m pretty sure this is
a basic UNIX security issue. I’m not sure that there’s a fix, sadly.
Hopefully I am wrong.
I’m thinking that the problem has to do with the process context that
the aliases are set in, but I don’t know how to fix the problem.
Ding. When you call system(), it fires up a subordinate shell which
runs whatever command you pass in. Then, that shell exits and your
aliases are gone.
What’s the problem you’re trying to solve? I think there’s probably a
better (non-Ruby) way to do what you want.
Ben
I’m basically trying to source a bunch of alias files without having to
resort to writing bash scripts. I’m hoping there is a way. Certainly,
if IO can be piped to the screen from the script then an alias can be
piped to the terminal session. Fingers crossed
Invoking the ruby interpreter to do this is pretty… crazy. It should
be trivial to write a bash script to iterate over a list and require
them all. I’m a tcsh user, otherwise I’d just give you the code
Stick all the alias files in a directory (.aliases.d) under your home,
then stick this in your bashrc:
for afile in ${HOME}/.aliases.d/*; do
if [[ -r $afile ]]; then
. $afile
fi
done
The alias gets set, I see the message ‘the alias file was run’ and the
system “source ~/.bash_aliases”
Now, each terminal session I open actually runs the file. I know this
because I get the message ‘the alias file was run’. However, the
terminal session doesn’t have access to the rt alias (command unknown).
I’m thinking that the problem has to do with the process context that
the aliases are set in, but I don’t know how to fix the problem.
Any ideas would be greatly appreciated. Thanks.
a shell must be interactive to obtain aliases. this will work
I’m basically trying to source a bunch of alias files without having to
resort to writing bash scripts. I’m hoping there is a way. Certainly,
if IO can be piped to the screen from the script then an alias can be
piped to the terminal session. Fingers crossed
Invoking the ruby interpreter to do this is pretty… crazy. It should
be trivial to write a bash script to iterate over a list and require
them all. I’m a tcsh user, otherwise I’d just give you the code
Ben
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.