12

Currently I'm setting up a new system using the new Xubuntu trusty tahr. I'm not very familiar with shell scripting, but I have one which needs the OSTYPE environment variable to determine what to do.

If I call echo $OSTYPE in the xfce-terminal I get succesfully linux-gnu.

If I call following script I only get an empty line.

#!/bin/sh
echo $OSTYPE

Am I missing something or is it maybe a problem of the new ubuntu?

On another machine of mine it works with that script. But I don't know if something was changed for that, because the system was originally not mine.

jww
  • 97,681
  • 90
  • 411
  • 885
keiki
  • 3,260
  • 3
  • 30
  • 38

1 Answers1

12

The OSTYPE environment variable is not recognized by the original Bourne shell, which is what is being invoked by the first line of your script.

Replace it with:

#!/bin/bash

or

#!/bin/ksh

as appropriate to your setup.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
rojomoke
  • 3,765
  • 2
  • 21
  • 30
  • I should have added that on my other machine it runs with /bin/sh. The other machine is using Ubuntu 13.10, but may have been altered without my knowledge. – keiki May 02 '14 at 12:02
  • In the end this was the hint in the correct direction. On my working machine /bin/sh was linked to /bin/bash . On the fresh installation /bin/sh is linked to /bin/dash – keiki May 02 '14 at 12:08
  • 2
    for portability: `#!/usr/bin/env bash` – Eliran Malka Mar 23 '17 at 12:05
  • 2
    ^ just a note..bash is not (for portability). as newer ubuntu uses dash for shell. if you need some better portability, ensure its bourne shell POSIX compliant . In bash you can enforce compliancy using "set -o posix" or running script with --posix . REF - https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html#Bash-POSIX-Mode – mirageglobe May 19 '18 at 10:16