Running bash command from Ruby (with your bash_profile)

… and check why 5600+ Rails engineers read also this

Running bash command from Ruby (with your bash_profile)

Whenever I try to automate some of my daily tasks, I end up with a mix of Ruby and Bash scripts. This is the time when I look up to the differences between system, exec, %x, backticks and others.

However, there’s additional thing with actually executing a bash script not just shell script.

Yesterday I’ve been optimizing my blogging flow. One part of it is to open my favourite editor with the current draft file.

Until yesterday I did it manually. I’ve had this in my bash_profile:

alias ia="open $1 -a /Applications/iA\\ Writer"

so just typing ia content/posts/ was opening the editor.

Now, I have a script which not only generates the draft file, but also git pushes it, opens the browser to preview it and opens the editor.

  def call

See my previous blogpost to read more about this specific Ruby service object

The thing is, if you just use system it’s not enough. You need to invoke bash in a special mode -ilc to actually get the bash_profile loaded. Otherwise, the ia alias is not recognized.

So, I ended up with this:

  def open_draft_in_editor
    system("bash", "-lic", "ia #{path}")

Which works great so far. It helped me speeding up my blogging process and hopefully will result in more blogposts ;)

Happy blogging!

BTW, if you want to improve your blogging skils, my “Blogging for Busy Programmers” book is now part (for a limited time) of the Smart Income For Developers bundle. Check it out!

You might also like