The following seems to work just fine in my sh
:
$ myvar="test"
$ echo "My var is $myvar."
My var is test.
There is no need to echo
the value of the variable and catch it again in another. Here is a little bit more information on variable assignments.
The thing is, OSTYPE
is not defined in sh
's environment. You'll have to pass it when running your script. So, instead of running...
$ /bin/sh yourscript.sh
or
$ ./yourscript.sh
You should run:
$ OSTYPE=$OSTYPE /bin/sh yourscript.sh
or
$ OSTYPE=$OSTYPE ./yourscript.sh
Of course, this assumes that the parent shell, in which you type the above command, has an OSTYPE
variable. While bash
does, it is not the case of every shell. Instead of $OSTYPE
, you might however be able to use uname
:
$ OS=$(uname -o)
$ echo "My OS is $OS."
My OS is GNU/Linux.
$ echo "My OS is $(uname -o)."
My OS is GNU/Linux.
On my machine, this sets the OS
variable to GNU/Linux
, instead of linux-gnu
(which is the content of my OSTYPE
).
As a side note, I'm guessing an equivalent of your code could be :
$ myvar="$(echo $OSTYPE)"
$ echo "My var is $myvar."
My var is linux-gnu
myvar="$OSTYPE"
– phemmer Oct 20 '14 at 16:57