0

I'm trying to set a Bash alias from a Ruby script. The intended functionality would be (from the Ruby script):

  • Open ~/.bash_aliases (or something)
  • Add alias line to file (e.g: alias foo="cd /bar/blah")
  • Source .bash_aliases
  • Exit Ruby script
  • Be able to use new alias

However, using the system command doesn't work because it launches a new subshell.

Any advice?

7
  • exec doesn't launch a sub-shell, I think. Commented Jun 25, 2015 at 1:09
  • Yes but exec also completely bypasses the rest of my script, because it replaces the current process, and I'd like to be able to print out some stuff before exiting. Also, I tried that, but the alias still doesn't work; not sure why. Commented Jun 25, 2015 at 13:31
  • Common sense, I'd guess. I wouldn't want some random script to be able to change my ENV, unless I specifically instruct it to Commented Jun 25, 2015 at 13:46
  • Agree, but what I was not sure about was why the alias doesn't work from a technical POV. Commented Jun 25, 2015 at 13:50
  • 2
    "why the alias doesn't work from a technical POV" - if by that you mean "why the alias doesn't persist after my program exits", the guess/answer would be the same: shell security (modified sub-ENV is discarded) Commented Jun 25, 2015 at 13:54

1 Answer 1

1

what you want to do is not doable.
the script you are launching cannot really alter the environment of the shell.
one way to do this would be to source the output of the ruby script and to have the script just generate the commands. This way you are instructing the shell to actually do the right thing.

something like

source $(my_ruby_script.rb)

have the script alter the aliases and at the end read and print out the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.