13

I'm looking at the source code of virtualenv, and the activate script contains this code:

if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then _OLD_VIRTUAL_PS1="$PS1" if [ "x" != x ] ; then PS1="$PS1" else if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi fi export PS1 fi 

What does the line if [ "x" != x ] do? x is not defined anywhere else in the script.

1 Answer 1

12

In Bash, that test is guaranteed to fail; [ "x" != x ] always returns a non-zero exit status (i.e. "false"), because "x" and x are both the string consisting of the single character x. (The quotation marks don't really have any effect in this case.)

What's more, the command PS1="$PS1" doesn't really do anything, either: it just sets the variable PS1 equal to the value it already has.

I'm guessing that this script is autogenerated in some way, and that on some systems, these statements will look a bit different, and a bit less useless.

Sign up to request clarification or add additional context in comments.

2 Comments

It looks like the old [ "x$foo" != x ] trick for checking for empty strings on old shells.
This is exactly what's going on. The script is auto-generated, and sometimes there's a variable that gets spliced in there. See this and this. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.