Shell script, sh and bash

Discussion in 'HOWTO-Related Questions' started by rodolfobsd, May 2, 2015.

  1. rodolfobsd

    rodolfobsd New Member

    Hi guy! :), I am learning shell script from howtoforge tutorial, in my case I use sh, my operating system has a bash by default, but I want to use sh, them I write an example:
    #!/bin/sh in the firt line, because in Debian I see in /bin sh also (/bin/sh) the same with bash, all a good many command is good, but when I use:
    #!/bin/sh
    echo $PWD
    echo $USER
    echo $OSTYPE

    sh or ./myscript.sh I see all good, but OSTYPE nothing :(, but when I use bash as my shell is good, but with sh nothing, I want to know, OSTYPE does not exit in sh shell? thank youfor you reply.
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    OSTYPE is a bash variable, please see:

    man bash

    command for a full list of all variables that are built into the bash shell. If you dont get an output for a variable in sh, then this variable does not exist in the sh shell.
     
    rodolfobsd likes this.
  3. rodolfobsd

    rodolfobsd New Member

    thanks a lot till for your reply
     
  4. sjau

    sjau Local Meanie Moderator

    IIRC default shell on debian is dash (/bin/sh is symlinked to it).
    If you want to use Bash then use
    Code:
    #!/usr/bin/env bash
    
    That's what is recommended by the #bash channel on freenode.

    I've found one case where /usr/bin/env is required and that's NixOS. NixOS provides atomic upgrades and hence binaries are no necessarlily in standard locations but in the nix store somewhere. So if you boot into a previous configuration or alternate configuration you could be using the wrong bash binary when you use /bin/bash. However the environment variable will use the correct one for that configuration.
     

Share This Page