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?


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
  • 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. Jul 8 '14 at 5:01

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.