Why statement works in LINUX and not UNIX?

Hello,

I have a ksh script that uses code below. For some reason it works under linux but fails in unix. Any idea why?

if [[ "$instance" =~ "DEV" ]]; then ...

Thanks

Post the respective shell versions.

=~ is BASH-only syntax, and only very new versions of BASH at that.

Most shells will let you do if [[ "$instance" == *DEV* ]] however.

1 Like

Thanks guys,
My linux bash version is
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

For unix, I can't even find one. I tried

bash --version
which bash
/bin/bash --version

When I do

man bash

it gives me No manual entry for bash. Is it possible that I do not even have one.

Yes, bash is a GNU program and is typically not installed on unix systems. ksh is quite similar and is a good alternative but some bash features will not be available (checkout this Bash/KSH Protability Issues link for some things to look out for).

$ echo $KSH_VERSION
@(#)MIRBSD KSH R48 2013/08/16
$  [[ "dfjdkfDEVfjfj" == *DEV* ]] && echo yes
yes

BASH is not the only shell in the universe, or even the only Bourne shell. sh is the ancestor of ksh and bash, which have different extended features on top of ordinary sh but remain mostly compatible with ordinary sh code.

bash incorporates many features of ksh, the == syntax I showed earlier being one of them.

Thank you all for your replies. I guess one thing I am confused about is if I run my script under Korn Shell (#!/bin/ksh) on both unix and linux then why does it matter if I have BASH installed and what version of BASH I have?
Thanks

I don't know exactly why bash came up, since your question is about ksh . On Linux (if ksh is installed) this is typically a recent version of ksh93 which supports the =~ syntax, whereas on many UNIX versions the default ksh is still ksh88 , which does not. Both the 88 and 93 version support the == syntax. On UNIX there may be a ksh93 version available but that depends on that particular OS and version.

1 Like

Thank you! this was really helpful. It all makes sense now. My UNIX is ksh88.

Not all versions of BASH have =~ either, just very new ones.

You are unlikely to have Korn installed on Linux unless you specifically ask for it to be installed, either.

KSH88 is a very popular variant of KSH for some reason, even though it lacks what I consider basic features ( i.e. substrings ).