Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm not exactly sure what I'm doing wrong with this one. I'm trying to run the command

alias localip='ip -4 -o addr show eth0 | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | head -n 1'

If I run the command

ip -4 -o addr show eth0 | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | head -n 1

I get the result I expect, however, when trying to create an alias with the command, I get

-bash: syntax error near unexpected token `('

Any help would be appreciated. TIA.

share|improve this question
up vote 3 down vote accepted

You're nesting single quotes within single quotes. That doesn't work.

Try using "double quotes" in the inner expression.

share|improve this answer
    
Or escaped single quotes. – Benjamin Bannier Mar 31 '12 at 22:16
    
Thank you soooo much. I knew it was something simple, I was just looking at it for too long. – n8felton Mar 31 '12 at 22:26
4  
You cannot escape single quotes inside a single quoted string: gnu.org/software/bash/manual/bashref.html#Single-Quotes – glenn jackman Apr 1 '12 at 1:56

I found it a much cleaner solution to just create a function and name your alias after the function, like this:

alias localip=GetLocalIP

function GetLocalIP()
{
   ip -4 -o addr show eth0 | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | head -n 1
}
share|improve this answer
    
What's the advantage of creating an alias to a function, rather than just naming the function localip? Also, the function keyword breaks compatibility with other shells. I'd suggest just using localip() { ... – Tom Fenech Jan 9 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .