Reputation: 77
Can someone explain to me why the following shell script line throws this error:
#!/bin/sh
var=$(uptime | awk ' { print $2 } ');
echo $var ;
Error:
syntax error 'var=$' unexpected
Upvotes: 3
Views: 1112
Reputation: 263307
Depending on the system and how it's configured, /bin/sh
doesn't necessarily understand the $(...)
syntax. Either change it to the older syntax:
var=`uptime | awk ' { print $2 } '`
or change the first line to
#!/bin/bash
(Yes, POSIX specifies the $(...)
command substitution syntax for sh
, but /bin/sh
isn't always POSIX-compliant.)
(Incidentally, the trailing semicolons are not necessary.)
Upvotes: 6