Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 am writing a shell script to start my network in my virtual machine on boot, since it does not do this right away for some reason with a snapshot of a virtual machine.

Since the eth device starts as being down, I have to get that device name with the following script and then start the device as well:

gateway=ifconfig -a | awk '/eth/ {print $1}'
dhclient $gateway

However I keep getting the following error, line 1: -a: command not found. ifconfig -a works however from the command line.

Is there a way to get ifconfig -a to work in my shell script?

share|improve this question
up vote 7 down vote accepted

You must use command substitution, otherwise, bash will think you assigned ifconfig result to variable gateway then run command -a:

gateway=$(ifconfig -a | awk '/eth/ {print $1}')
dhclient $gateway
share|improve this answer
thank you that works, I knew it had to do with how it was interpreting the script but couldn't pinpoint it. – jgr208 Jul 7 '14 at 16:14
The right single quote is wrongly placed. For some reason, stack exchange is too stupid to accept a minor edit. – Pavel Šimerda Jul 8 '14 at 5:01
Thanks, I updated my answer and OP question. – cuonglm Jul 8 '14 at 5:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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