osx - How to define a series of bash commands as a string? -


right of code in .zshrc file looks this:

inetfunction(){     echo ${lred}ip address:${nc}     ifconfig en0 | grep "inet " | awk '{print $2}' } 

$lred defined changing color light red, , $nc setting normal. implement inetfunction code alias inet='inetfunction'. output of command is

ip address: xx.x.xx.xxx 

where "ip address:" in red. wanted make ip address green, reason, when try of these, doesn't work:

${green}ifconfig en0 | grep "inet " | awk '{print $2}'  ifconfig en0 | grep "inet " | ${green}awk '{print $2}'  ${green}ifconfig en0 | grep "inet " | awk '{${green} print $2}' 

i tried setting whole thing variable like:

variable='ifconfig en0 | grep "inet " | awk '{print $2}'' 

and trying echo ${green} $variable, still doesn't work.

any ideas?

$green needs echoed. it's set of ansi color codes terminal recognizes signal change text color.

echo -n "$green" ifconfig en0 | grep "inet " | awk '{print $2}' echo -n "$nc" 

or, condensed:

echo "$green$(ifconfig en0 | grep "inet " | awk '{print $2}')$nc" 

also, @jens points out can combine grep , awk commands so:

echo "$green$(ifconfig en0 | awk '/inet / {print $2}')$nc" 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -