If you read the manual for the bash shell, double-quoting preserves the literal value of characters within the quotes, except for '$', '`' and '\'. So, the line you wrote is replacing $1
with the first argument of the first script, instead of printing a literal '$1' in the second script. I believe what you want is this:
#! /bin/bash
MY_FILE=$1
MY_PATH=$2
touch "$MY_PATH"/"$MY_FILE"
echo "#! /bin/bash" > "$MY_FILE"
echo `ps -u $1 -o comm=CMD -o pid,ppid,user,uid,gid` >> "$MY_FILE"
Note that I have also added double quotes around the variable expansions, such as "$MY_FILE"
. This is to avoid confusion, in case the stored strings include spaces.
However, I have to say that I don't understand what you are trying to achieve here:
a) The commands you are printing to the second script don't depend on the arguments to the first script (in which case, you could simply copy the second script, if you need to duplicate it).
b) The file you have touched with touch "$MY_PATH"/"$MY_FILE"
could be a completely different file to the one you have printed the other commands to, since you have not included the "$MY_PATH"
variable in those calls. Perhaps that is the intended behaviour, but it seems a little strange.