LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2011, 09:51 PM   #1
martindl
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Using an alias inside a function in bash


Hi,

I have trouble with using an alias inside a bash function. I would like to ssh into multiple machines by executing:

ssh machine

To achieve this, I put something like the following into my ~/.bashrc:

alias machine='user@machine'

ssh()
{
eval "/usr/bin/ssh $1"
}

When I type

ssh turing

I would expect a prompt for a password. Instead I get

ssh: Could not resolve hostname turing: Name or service not known

which suggests to me that the alias turing does not get expanded. Is it possible to use an alias inside a function? How can I set up my .bashrc so that

ssh machine

does what I want?

Thanks
 
Old 06-08-2011, 11:55 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
An alias is a command substitution. But you're trying to use it as a substitution for an argument to another command. It just doesn't work that way, whether in a function or not. In this case a simple variable substitution is what you want to use.
Code:
machine='user@machine'

ssh()
{
command ssh "$1"
}

ssh $machine
Edit: another option is to make the entire function call the alias:
Code:
ssh()
{
command ssh "$1"
}

alias sshmachine='ssh user@machine'

sshmachine

PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

PPS: It's generally recommended to avoid the use of eval whenever possible. In this case it's completely unnecessary, especially since you're giving the complete path to the executable. But if you need to avoid a conflict between the ssh command and the ssh function (or an alias), use the command built-in. I've updated the examples above to demonstrate this.

Last edited by David the H.; 06-09-2011 at 01:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using shortcuts/alias inside a Linux Bash Command pratap.iisc Linux - General 3 01-30-2010 04:52 AM
[SOLVED] [bash] alias + function = weird... RaptorX Programming 4 08-01-2009 06:36 PM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
function calls inside a function manas_sem Programming 2 02-28-2007 01:27 AM
alias not workin inside a script sanjith11 Programming 7 08-31-2005 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration