#note: no quotes
#note: with quotes, but not good for filesnames, rather use "$filename" in the command argument
DIRnew='"$(cd "$(dirname "$0")" \&\& pwd -P)"'
"$(cd "$(dirname "$0")" \&\& pwd -P)"
#note: with quotes, escaping & needed for suppressing interpretation by sed
DIRold='"$(dirname "$(readlink -f $0)")"'
"$(dirname "$(readlink -f $0)")"
#note: in your question the original DIR=.. did NOT have double quotes,
#I assume a typo
sed -e "s/$DIRold/$DIRnew/g" "$OrigScript"
#note: $OrigScript should NOT have the double quotes in it!
#note: I assume $OrigScript == $StartStopScript
echo $DIRold | sed "s/$DIRold/$DIRnew/"
"$(cd "$(dirname "$0")" && pwd -P)"
#result includes double quotes!
1) for declaring a variable with (double) quotes, you need to escape the quotes to have the quotes in the string, better use single quotes to make sure the string inside is exactly just the string and no variables are interpreted. This also simplifies the use of
$() as the are then not interpreted by the shell
echoing (double) quotes, escape them
3) for allowing
sed to interpret a shell variable, you MUST use double quotes around the
sed command ( with single quotes, the command
sed 's/$a/$b/' will try to replace the actual string '$a' with the string '$b' , also be aware of the special meaning of
sed), double quotes around the variable within the
sed command is not needed, as
sed is for treating strings with any character (with restrictions regarding the special command characters of
echo "$StartStopScript" | sed -e 's/"$DIRold"/"$DIRnew"/g' will echo the variable
$StartStopScript ( which I assume is simply the file name of the script ) and try to replace the variables in that string (i.e. the file name). For applying
sed to a file, use
sed 's/a/b/g' file (output written to
stout), for overwriting the file, use
-i (use with care, i.e. test for the right substitutions first)
& has a special meaning in
sed, it reprints the initial (matching) pattern:
echo ABC | sed 's/A/&12/'
You will have to escape it in the replacement pattern to suppress interpretation:
echo ABC | sed 's/A/\&12/'
Thus the variable
$DIRnew needs to have the escape backslashes in its string.
6) Apart from that, I would suggest to rather use an
if loop identifying the OS and to adapt the commands accordingly. That way you only need one script for both OSes. I.e.
if [ OS = Mac ] ; then