I'm peeking through some shell scripts - what's the purpose of the x in the comarison shcu as
if [ "x$USER" != "x$RUN_AS_USER" ]; then
su - $RUN_AS_USER -c "$CATALINA_HOME/bin/startup.sh"
else
$CATALINA_HOME/bin/startup.sh
fi
Bash Shell -x Option. Invoking a Bash shell with the -x option causes each shell command to be printed before it is executed.
X is used to indicate all operating systems similar to Unix.
$? returns the exit value of the last executed command.
Bash provides the set command in order to enable or disable different features. The set -x command is used to debug bash script where every executed statement is printed to the shell for debugging or troubleshooting purposes.
It's a trick to ensure you don't get an empty string in the substitution if one of the variables is empty. By putting x
on both sides it's the same as just comparing the variables directly but the two sides will always be non-empty.
It's an old kludge which made more sense when scripts were written as:
if [ x$USER != x$RUN_AS_USER ]
There if you just had $USER
and it were empty then you could end up with
if [ != root ] # Syntax error
With the x
you get this, which is better:
if [ x != xroot ]
However, when the variables are quoted the x
is unnecessary since an empty string in quotes isn't removed entirely. It still shows up as a token. Thus
if [ "$USER" != "$RUN_AS_USER" ] # Best
is the best way to write this. In the worst case with both variables empty you'd get this which is a valid statement:
if [ "" != "" ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With