1
sudo ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

The above command when run from terminal outputs the right "internal" ip address. When I try to pass as alias like:

alias intip='sudo ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}''

I am getting the following error:

alias: addr: | cut -d: -f2 | awk { not found
alias: print not found
alias: } not found

What I am doing wrong here?

  • 1
    The alias end as grep . Use 'quotes "inside" quotes' or use "quotes 'inside' quotes" to address this. – Michael Durrant Jun 28 '13 at 19:37
  • 1
    There is no reason to use sudo for this, FYI. At least not on Linux. You just need the path: /sbin/ifconfig (/sbin is not in non-root users' paths by default) – derobert Jun 28 '13 at 21:46
  • Its not a regular pc situation. Its Nokia N900 with the Maemo Operating system where the regular user has no access to ifconfig and other utils ;-) – Vaios Argiropoulos Jun 29 '13 at 8:10
3

A matter of using double quotes and single quotes where it matters:

alias lsa="ls -l | awk '{print \$1}' "
  • ;-) Great, glad you made it – GermanG Jun 28 '13 at 19:25
3

What you're doing wrong is using an alias for something that's more complicated than giving a shorter name to a command or passing parameters to a command automatically. Use a function instead, and you won't have to worry about quoting.

intip () {
    /sbin/ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
}

Note that you don't need sudo here, ifconfig doesn't need privileges when you're only looking. But on many distributions you need to provide the full path as ifconfig is often not in the default PATH for non-root users.

By the way, you can do your filtering entirely in awk, though feel free to use multiple tools if you're more comfortable that way. Here's one way:

intip () {
    /sbin/ifconfig wlan0 | awk 'sub(/^.*inet addr:/,"") {sub(/ .*/,""); print}'
}

You can easily make the interface name an optional parameter of the function.

intip () {
    /sbin/ifconfig "${1:-wlan0}" |
    awk 'sub(/^.*inet addr:/,"") {sub(/ .*/,""); print}'
}

Just to show why your alias doesn't work: you have single quotes both to delimit the alias and inside the alias text. The interpreter can't read your mind and see which ones were meant to be which. See Calling bash from sh (dash) with commands read from args, and "Unterminated quoted string"/"unexpected EOF" for some quotes-inside-quotes cases and Why is echo ignoring my quote characters? for how to effectively include a single quote in a single-quoted string by entering the four characters '\''.

2

Or you could install moreutils (at least, if its available for your distro) and use ifdata.

$ ifdata -pa br0
172.16.1.244

You'd use wlan0 instead of br0, of course.

  • Wow, I had no idea this was in moreutils. Thanks! However, nowadays it seems ifconfig is deprecated on most systems in favor of the newer ip command, but I don't know much more about it than that. – jw013 Jun 28 '13 at 22:00
  • 1
    @jw013 ip has many, many more features—required in advanced networking—than ifconfig, which is why the later is obsolete. But ifdata isn't actually using ifconfig, at least not for getting the interface address. Its using ioctl(SIOCGIFADDR, …) – derobert Jun 28 '13 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.