0

I am new to linux and trying to create an alias that starts mongodb service. The original command is sudo service mongod start. I want to generalise this usage for any service.

i.e. something like alias startservice="echo <password> | sudo -S $1 start".

So I would call it like startservice mongod should run the first command. I came to know that I can use functions for the same. However, I don't have a clue on how to do this either way.

Because I want my function which I create to be able to be accessible across terminals. I am not sure on how to create functions that act in this manner. Please help me on this.

I have gone through these two links:

parameter subsitution in bash aliases

Alias with Argument in Bash - Mac

Your help would be appreciated.

Leela Venkatesh K
  • 1,390
  • 1
  • 13
  • 26
  • 1
    You want to do a function, not an alias – Arount Jan 31 '18 at 11:34
  • @Arount I understand that, however, I am not sure how to add a global function. that is what I want to know about. Thanks – Leela Venkatesh K Feb 03 '18 at 04:37
  • Just create a file, like `~/.custom_functions` with your functions and at the end of your `~/.bashrc` add this line: `source ~/.custom_functions` so your functions will be "loaded" when you login to your account and will be ready to be used. – Arount Feb 03 '18 at 12:58

1 Answers1

0

Example of function in bash:

startService(){
  echo "your-password" | sudo -S service "$1" start
}

startService mongod                                    # test
Bach Lien
  • 1,030
  • 6
  • 7