Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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


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'

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?

Old 06-08-2011, 11:55 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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.

command ssh "$1"

ssh $machine
Edit: another option is to make the entire function call the alias:
command ssh "$1"

alias sshmachine='ssh user@machine'


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.


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 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:40 PM.

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