LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 10: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-09-2011, 12:55 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 02:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:52 AM
[SOLVED] [bash] alias + function = weird... RaptorX Programming 4 08-01-2009 07:36 PM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 07:08 AM
function calls inside a function manas_sem Programming 2 02-28-2007 02:27 AM
alias not workin inside a script sanjith11 Programming 7 08-31-2005 04:36 PM


All times are GMT -5. The time now is 06:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration