Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does if [ "x" != x ] do in bash?

Tags:

shell

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.

like image 916
A Kaptur Avatar asked Dec 20 '12 16:12

A Kaptur


1 Answers

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.

like image 123
ruakh Avatar answered Oct 29 '22 17:10

ruakh