0

I'm reading Gitlab's source code to learn more about how it works. In the run bash script located inside the project root directory, I see the following:

if [ "x$GDK_RUNIT" = "x1" ]; then
  ...
fi

I know this conditional's purpose is to check if the value of the GDK_RUNIT envar is set equal to 1, and to exit with a non-zero return code if that's the case. My question is, what is the difference between the above code and the following:

if ["$GDK_RUNIT" = "1" ]; then
  ...
fi

In other words, what is the purpose of placing "x" before both the envar name and "1"?

I checked man test for anything related to x (since I know if and test are functionally equivalent), but all I saw was the -x flag.

x$GDK_RUNIT doesn't look like a flag to me, so I assumed the contents of the man page wasn't relevant.

Richie Thomas
  • 3,073
  • 4
  • 32
  • 55

1 Answers1

2

It's completely unnecessary. There are old implementations of test which cannot handle an empty argument, so if GDK_RUNIT is undefined or empty, it avoids the equivalent of [ "" = "1" ], replacing it with [ "x" = "x1" ] instead. Just about any character or string would work in place of x, but the use of x was conventional.

However, no reasonably modern implementation, and certainly not bash's, has this problem.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • From [this link](https://stackoverflow.com/questions/174119/why-do-shell-script-comparisons-often-use-xvar-xyes), I read that a 2nd reason for this convention is that the conditional's meaning may be ambiguous if the value of the envar is set to something that resembles a flag, such as `-n`. Is this edge case also handled by modern implementations of `test`? If not, does this imply that a good reason still exists to use this convention? – Richie Thomas Aug 25 '19 at 22:42
  • 2
    Modern versions also use the *number* of arguments as the starting point for interpreting them. With three arguments, only the second one is considered as an operator; all the operator names start with a hyphen, but the hyphen isn't what makes them operators. – chepner Aug 25 '19 at 22:52
  • 2
    @RichieThomas, ...incidentally, what chepner just described above -- number-of-arguments being used for interpretation -- is part of why POSIX flags use of `-o` and `-a`, and other operators that lead to uses with more than three arguments excluding a possible leading `!` and trailing `]` if called under the name `[`, as obsolescent (as they reintroduce ambiguities that don't exist if they're excluded). – Charles Duffy Aug 25 '19 at 23:03
  • @chepner `...all the operator names start with a hyphen, but the hyphen isn't what makes them operators.` So in the phrase `(test $num -gt 5)`, you mean to say it's the *position* that makes `-gt` an operator, *not* the fact that it has a leading dash character? – Richie Thomas Aug 26 '19 at 06:16
  • @CharlesDuffy `operators that lead to uses with more than three arguments`... are you referring to something like `(test $num -gt 5)`, but somehow passing more than 3 arguments to `test`? What would be an example of that? I know you said this scenario is obsolete at this point, but you've piqued my curiosity and I'm not the type who can stop pulling a thread once I've started. :-) – Richie Thomas Aug 26 '19 at 06:26
  • 1
    @RichieThomas, `test "$num" -gt 5 -a "$num" -lt 10` is an example; as opposed to the preferred syntax of `test "$num" -gt 5 && test "$num" -lt 10`. Once you've introduced `-a` and `-o` for "and" and "or", it makes other syntax, like `'('` and `')'` for grouping, in-play, which complicates correct parsing in light of ambiguous data values substantially. – Charles Duffy Aug 26 '19 at 07:52
  • @RichieThomas : In addition to the correct response by chepner, another difference is that your second version would produce the error message _bash: [: =: unary operator expected_, due to the lacking space, while the first version is syntactically correct. – user1934428 Aug 26 '19 at 08:06
  • 1
    @user1934428, ...in the empty case, yes; in the non-empty case, it would be a command-not-found. – Charles Duffy Aug 26 '19 at 08:15