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 '\''
.
grep
. Use 'quotes "inside" quotes' or use "quotes 'inside' quotes" to address this. – Michael Durrant Jun 28 '13 at 19:37sudo
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