如何使ruby的`system`调用知道我的`.bash_profile`别名? [英] How do I make ruby's `system` call aware of my `.bash_profile` aliases?

查看:56
本文介绍了如何使ruby的`system`调用知道我的`.bash_profile`别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从ruby程序中调用我的快捷方式之一(即别名).换句话说,我想要

I'd like to be able to call one of my shortcuts (i.e., aliases) from within a ruby program. In other words, I want

system('cpl')

相当于写作

cd ~/Documents/CPlusPlus

在命令行中,因为我的 .bash_profile 包含该行

at the command line, because my .bash_profile includes the line

alias cpl="cd ~/Documents/cplusplus"

我使用的是Mac OSX,而​​我的 .bash_profile 存放在通常的位置()中,则可能是在任何旧文件夹中写入了ruby.我正在使用/usr/local/bin/ruby​​ 中的Ruby 2.2.0.

I'm on a Mac OSX, and while my .bash_profile lives in the usual place (~), I might be writing ruby in/to any old folder. I am using Ruby 2.2.0 which is located in /usr/local/bin/ruby.

推荐答案

来自文档:

在当前Shell上下文中从filename参数读取并执行命令.

Read and execute commands from the filename argument in the current shell context.

来自 shopt -s expand_aliases 文档:

如果设置,别名将被扩展.交互式外壳默认情况下启用此选项.

If set, aliases are expanded. This option is enabled by default for interactive shells.

您使用非交互式外壳,因此需要执行此附加步骤:

You use non-interactive shell, so this additional step is required:

system %(
  source ~/.bash_profile
  shopt -s expand_aliases
  cpl
)

这篇关于如何使ruby的`system`调用知道我的`.bash_profile`别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆