Search code examples
bashshsubstitutionstring-substitutionparameter-expansion

Bad subsitution when trying to do string replacement with parameter expansion


Maybe this is a dumb question, but I'm writing a very small Gedit external tool for compiling .qrc (Qt resource files) on a key press. This is my code:

echo $GEDIT_CURRENT_DOCUMENT_PATH
pyrcc $GEDIT_CURRENT_DOCUMENT_PATH -o ${GEDIT_CURRENT_DOCUMENT_PATH/.qrc/.py}

Running the above, I get

/home/user/.local/lib/python3/qrecartivi/resources.qrc
/home/user/.config/gedit/tools/new-tool: 12: /home/user/.config/gedit/tools/new-tool: Bad substitution

where line 12 actually is the 2n line of my sh code (This offset is due to gedit adding some comments in). I just cannot figure out why this substition should be wrong. Thanks in advance.


Solution

  • As stated in comment by @chepner, the syntax ${var/str1/str2} is not supported by your shell interpreter (Dash) which is strictly POSIX-compliant.

    You have two solutions:

    • Use Bash which supports this kind of variable substitution

    • Use a POSIX-compliant way to substitute an extension with another. Like this for example:

    pyrcc "$GEDIT_CURRENT_DOCUMENT_PATH" -o "$(basename "$GEDIT_CURRENT_DOCUMENT_PATH" .qrc).py"