I need to make a bash script to replace a certain application.
Script should do some work and then execute said application in a way that everything (parameters both positional and named, env variables, signals etc.) will work as if it was the original application executed.
My idea was to do it like this:
#!/bin/bash
# Do something custom
export PATH=/some/custom/override:$PATH
# Execute the original script
exec php8.1 $@
Is this enough or should I do something else to ensure a perfect substitution?
CodePudding user response:
You should check your scripts with shellcheck .
Line 7:
exec php8.1 $@
^-- SC2068 (error): Double quote array expansions to avoid re-splitting elements.
Use:
exec php8.1 "$@"
CodePudding user response:
If PATH is empty or unset, your code adds an empty path entry (after
:
), causing the current directory to always be in PATH.You should set PATH like:
export PATH=/my/path${PATH :$PATH}
, orexport PATH=/my/path${PATH: :$PATH}
.The first one avoids an empty entry if PATH is unset, but not if it has already been set to empty. The second one always avoids an empty PATH entry.
Although it's unlikely PATH will be unset or empty, if you want "perfect substitution", you should use the first example. If you believe empty PATH is always a bug for your application, you could use the second example.
Apart from that,
exec command "$@"
is pretty standard (add quotes as already mentioned).