views:

53

answers:

3

Hello,

Is it possible to do the following:

I want to run the following: mongodb bin/mongod

in my bash_profile I have

alias = "./path/to/mongodb/$1"

Thanks everyone!

A: 

what you ask is very unclear, but I guess you will have better luck with functions. aliases are not very flexible

Stefano Borini
A: 

Usually when I want to pass arguments to an alias in Bash, I use a combination of an alias and a function like this, for instance:

function __t2d {                                                                
         if [ "$1x" != 'x' ]; then                                              
            date -d "@$1"                                                       
         fi                                                                     
} 

alias t2d='__t2d'                                                               
leed25d
What is that always-false `if` statement for?
Dennis Williamson
fixed. It was a typo, sorry. it was really meant to check for the existence of $1
leed25d
`[ -n "$1" ]` (the use of "x" in that manner is archaic and unnecessary for any modern shell)
Dennis Williamson
Well, you can call me Old School. The original question dealt with aliases and arguments to them. I believe that your remark contributes nothing germane to the conversation.
leed25d
+3  A: 

An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1).

$ alias foo='/path/to/bar'
$ foo some args

will get expanded to

$ /path/bar some args

If you want to use explicit arguments, you'll need to use a function

$ foo () { /path/to/bar "$@" fixed args; }
$ foo abc 123

will be executed as if you had done

$ /path/to/bar abc 123 fixed args
Dennis Williamson
Surely you meant `"$@"`?
Roman Cheplyaka
@Roman: Corrected, thanks.
Dennis Williamson