|
From: Kevin Z. <kev...@gm...> - 2016-07-28 22:28:57
|
On 07/28/2016 15:24, Jef Poskanzer wrote: >> What's the purpose of the 'x' in lines like these? >> >> if [ "x$2" = "x6" ]; then >> >> Since the string literals are provided, there shouldn't be an error when >> the provided strings are empty. Isn't the 'x' there to guard against an >> empty string? > > That's a common idiom in shell script programming. It's to > guard against the string being check having a flag argument > that the [ command, a.k.a. test, would interpret. This > is really due to test having poorly thought out argument > syntax but what are you gonna do. Ahh, I see. I'm glad someone is looking over my shell scripts. > Another common idiom is to merely reverse the order of > the strings being checked: > > if [ "6" = "$2" ]; then > > Putting the variable second means test won't try to interpret > it as a flag. I think I like this better. Thanks, Kevin -- Kevin Zheng kev...@gm... | ke...@be... | PGP: 0xC22E1090 |