How to pass command line arguments to a shell alias?


Connect to a remote host using a function-based alias:

Scenario: Assume, you are connecting to a remote host. Here I am connecting to a GCP host.

$ ssh user_name@127.0.0.1

I would like to create a function alias "gssh" so that if I provide an IP, it should be able to connect to the GCP host, instead of typing the full command every time.

For that, let's add the below code to the .bashrc file (or) the file which is having alias information.

vi .bashrc

function gssh()
{
ssh -o stricthostkeychecking=no user_name@$1
}
alias gssh='gssh'

[OR]

Below code, If you are using the private key
function gssh()
{
ssh -o stricthostkeychecking=no -i /Users/user_name/.ssh/id_rsa user_name@$1
}
alias gssh='gssh'

Note: Replace user_name accordingly in the above code.

Now, load the file once.
$ source .bashrc
Let's connect using the alias:

$ gssh 127.0.0.1

We should be able to connect using the function alias we created.

───────────────────────

DISCLAIMER

The purpose of sharing the content on this website is to Educate. The author/owner of the content does not warrant that the information provided on this website is fully complete and shall not be responsible for any errors or omissions. The author/owner shall have neither liability nor responsibility to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the contents of this website. So, use the content of this website at your own risk.

This content has been shared under Educational And Non-Profit Purposes Only. No Copyright Infringement Intended, All Rights Reserved to the Actual Owner.

For Copyright Content Removal Please Contact us by Email at besttechreads[at]gmail.com


Post a Comment

Previous Post Next Post