user3595099
user3595099

Reputation: 77

shell script syntax error 'var=$' unexpected

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

Answers (1)

Keith Thompson
Keith Thompson

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

Related Questions