Jim
Jim

Reputation: 21

Simple shell script works in cygwin but not on solaris

I'm trying to store the output of a bash command in a variable. I'm pretty new to shell scripting, but so far, I have the following:

#!/bin/sh
MOSTRECENTMOD="$(echo | ls -t | head -n1)"
echo "$MOSTRECENTMOD"

This works just fine in cygwin, but not on Solaris. Any ideas? I am using Unix newlines. I tried a lot of things, but it just doesn't seem to work.

On Solaris, it just outputs:

$(echo | ls -t | head -n1)

when run.

Thank you.

Upvotes: 2

Views: 496

Answers (4)

jlliagre
jlliagre

Reputation: 30813

Solaris 10 and older /bin/sh isn't POSIX compatible for legacy/upward compatibility reasons. If you want to keep your script unchanged, which I would recommend, you need to switch to a shell that support the posix $() notation, like /bin/ksh, /usr/xpg4/bin/sh or bash.

eg:

#!/bin/xpg4/bin/sh
MOSTRECENTMOD="$(echo | ls -t | head -n1)"
echo "$MOSTRECENTMOD"

Note also that Solaris 11 (Express) has a compatible /bin/sh so this won't be required.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753605

The standard /bin/sh on Solaris is fairly strictly a System V Bourne Shell. It is categorically not a POSIX-compatible shell, and does not understand the $(...) notation (amongst quite a number of other differences, of greater or lesser importance). That means to continue use /bin/sh, you will need to change the $(..) notation to use backticks (which are a pain to show in inline Markdown - I've tried all sorts of sequences without success:

`...`

Alternatively, if your machine has /bin/bash, specify that on the shebang line; otherwise, specify /bin/ksh which does support $(...) notation and many other useful features.

Upvotes: 4

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

I believe the $( ... ) syntax is from Bash, not standard sh. Use backquotes (`echo | ls -t | head -n1`) or change #!/bin/sh to #!/bin/bash (assuming that exists on your system).

Upvotes: 2

Franci Penov
Franci Penov

Reputation: 75991

I think your problem is that you have too many quotes. Get rid of the quotes on the first line, and it should work.

Upvotes: 0

Related Questions