Search code examples
linuxcsh

Bad : modifier in $ ($)


I have written small script and getting bad mofied error. not able to figure out what i am missing here. the cth_pythonpath variable exist into environment and able to print variable

#!/bin/csh -f

set script_rootdir = `dirname $0`

set script_abs_rootdir = `realpath $script_rootdir`

setenv PYTHONPATH $CTH_PYTHONPATH:$PYTHONPATH

Error:

Bad : modifier in $ ($).

Solution

  • csh allows variable modifiers in the form of $varname:x, where x is a letter. Some examples are :e to get the extension, :q to quote the string, and a whole bunch more. For example:

    > set path = 'file.txt'
    > echo $path:e
    txt
    

    The problem you're running in to is that this:

    setenv PYTHONPATH $CTH_PYTHONPATH:$PYTHONPATH
    

    That the same syntax: $CTH_PYTHONPATH ends with a :, so it tries to apply the $ modifier, but this modifier doesn't exist so you get the error:

    Bad : modifier in $ ($).
    

    The solution is to use ${varname} to explicitly tell csh when the variable name ends or using quotes:

    setenv PYTHONPATH ${CTH_PYTHONPATH}:${PYTHONPATH}
    setenv PYTHONPATH "$CTH_PYTHONPATH":"$PYTHONPATH"
    

    I recommend quoting variables by the way; it will prevent problems with spaces and the like; I'd probably write this as:

    setenv PYTHONPATH "${CTH_PYTHONPATH}:${PYTHONPATH}"
    

    Note that I don't recommend using csh if you can help it by the way; it's an old shell with a lot of problems, including strange errors in all sorts of cases. Note how the error you have doesn't print the line number or is really very helpful.