Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create an alias for grep like this:

grep argX ~/myfile

where argX is a parameter and myfile is always the same. How can I do this?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Alias don't supports parameter but you can write a small script and name it i.e. "filegrep"

#!/bin/bash
grep $1 /home/youruser/myfile

Copy the script to /usr/bin and you can run it with filegrep argX in the console.

share|improve this answer
add comment

aliases do not support the positional parameters so you need to create a function (in ~/.profile) and alias that function.

function grepMe(){
    grep "$1" ~/myfile
}

and then alias it..

alias grepAlias="grepMe"
share|improve this answer
1  
+1. Functions are the more generalized aliases suitable for this. And just to make the message clear: you don't have to create an alias; the function name will work fine in the command line. –  progo May 28 '13 at 14:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.