The [[
construct doesn't exist in all sh variants. It's a ksh thing that was adopted by bash and zsh. FreeBSD's sh is an ash derivative and doesn't support [[
.
If you want to use [[
in a script, use a shebang line that calls a shell that supports [[
. You can install bash or ksh93 or mksh as a package on FreeBSD; all of them support [[
. Packages are installed under /usr/local/bin
, whereas bash on Linux is almost always in /bin
; you can use /usr/bin/env
to call a program in the PATH
(it's a classic hack). So if you want to use [[
and other bash constructs, make sure that the bash package is installed and start your script with
#!/usr/bin/env bash
Alternatively, create a symbolic link /usr/local/bin/bash -> /bin/bash
on Linux, and start your scripts with
#!/usr/local/bin/bash
or create a symbolic link /bin/bash -> /usr/local/bin/bash
on FreeBSD and start your scripts with #!/bin/bash
.
Alternatively, write scripts that stick to the common sh core. Use [
instead of [[
. Beware that [
is an ordinary command, not a reserved word, so what's inside single brackets is parsed normally, unlike what's inside double brackets — for example you can't write [[ condition1 && condition2 ]]
in the same way with single brackets, you need to write [ condition1 ] && [ condition2 ]
. Plain sh doesn't have a regular expression matching construct, but for what you're doing, wildcard pattern matching is sufficient:
case "$1" in
*[\\/]) echo "$1 ends with a slash or backslash";;
*) echo "$1 does not end with a slash or backslash";;
esac
echo $BASH_VERSION
or any similar would say it.