Bash solution:
if [[ $HOSTNAME == 'foo' ]]; then
echo 'yep this is foo'
else
echo 'not foo'
fi
In regard to the response from @alper :
You can combine your logical OR (||) like this:
if [[ $(hostname) == "home" || $(hostname) == "home2" ]]; then
echo "True"
fi
It's fine to do it the way you did, but I thought you might want to know you can save some keystrokes.
One other critique is that using a subshell $() to execute the hostname command is slower than comparing against $HOSTNAME. There are valid reasons to do this in some environments, but the builtin environment variable $HOSTNAME should suffice for most simple use cases.
In response to @Jens:
Bash supports the POSIX-compliant string-equality comparison operator (=), but has its own string-equality comparison operator (==) to differentiate it from the POSIX-compliant operator. This mirrors how bash's built-in test construct ([[ ]]) is doubled to differentiate it from the POSIX-compliant test contract ([ ]).
Calling bash a 'buggy shell' because it 'accepts' its own string-equality construct is a complete misrepresentation.
Saying that '==' is accepted by 'buggy shells in an attempt to ease programmers with less error messages' is totally false, not to mention bad grammar. You meant 'fewer' error messages.
Saying that '== is a bug' and 'some shells only accept it because of widespread ignorance' is completely wrong.
Solaris is being replaced with Linux more and more with each passing year in corporate environments. The majority of *nix servers run some flavor of Linux. So POSIX compliance in all shell scripts is no longer a hard requirement, and is increasingly irrelevant for a lot of environments.
Bash won the shell wars, because Linux won the *nix wars.
[x
is different from[ x
. – Loring=
, not==
, even though the latter is accepted by buggy shells in a misguided attempt to ease programmers with less error messages. – Loring==
is a fairly standard equality operator syntax? I'm a software engineer now really using shell a lot these days and I'm just curious. – Electrotype=
is the comparison operator fortest
. Many years ago some shells started to make==
synonymous. This made scripts using==
suddenly non-portable and ill-behaved on conformant shells (of which there were many, and some still are around). – Loring