Home > bash, python > alias with an argument

alias with an argument

Problem
In bash, you want to write an alias that receives an argument.

Solution
Well, you can’t do that in bash. But there is a workaround: call a bash function, and the function can treat the argument.

Let’s see a concrete example. While working with Python virtual environments, I wanted to be able to activate a virtual env. easily with the command “workon <DIR>“, where “workon” would be an alias and “<DIR>” is the name of a directory. (Detailed post is here.)

Add the following lines to your ~/.bashrc:

func_workon()
{
if [[ -z "$1" ]]
then
    echo "Usage: workon <venv>"
else
    . $1/bin/activate
fi
}
alias workon=func_workon
Categories: bash, python Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: