Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script issue

I can run this command fine, with the output I want:

ifconfig eth0 | grep HWaddr | awk '{print $5}'

However, when I set the command to a variable, and print the variable, I get an error:

CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
$CASS_INTERNAL

my internal xxx ip: command not found

The weird thing - my internal IP actually shows up. How do I go about this without getting an error? It shouldn't matter, but I'm using the latest version of Ubuntu.


2 Answers

You're not printing the variable, you're running it as a command name. You're looking for

echo "$CASS_INTERNAL"

(Get into the habit of always putting double quotes around variable substitutions.)

More advanced shell note: in this case it doesn't matter, but in general echo can have trouble with some special characters (- and \\), so it's better to use the following more complicated but fully reliable command:

printf "%s\n" "$CASS_INTERNAL"
like image 172
Gilles 'SO- stop being evil' Avatar answered Aug 14 '26 09:08

Gilles 'SO- stop being evil'


don't have to use grep

ifconfig eth0 | awk '/HWaddr/{print $5}'
like image 30
ghostdog74 Avatar answered Aug 14 '26 09:08

ghostdog74