Question

I'm new to shell scripting. I've started using terminal a lot for SASS and git and want a simple script to make creating shortcuts easy.

function ma {
    cmd="echo alias $1='$2' >> ~/.bash_profile";
    cmd=${cmd/'$pwd'/$(pwd)};
    eval $cmd;
    echo $cmd;
}

The problem is that the single-quotes are not written to .bash_profile.

In example ma mycd '$pwd', the echo'd version is correct:

echo alias mycd='cd /current/working/directory' >> ~/.bash_profile

but this is written to the file:

alias mycd=cd /current/working/directory

I have tried maybe 50 permutations of this, trying to get the right combination of single and double quotes, and escaped quote, in the string or in the eval statement. How can I make eval keep those single quotes, or what should I use instead? Thanks!

Was it helpful?

Solution

Try the following:

 cmd="echo \"alias $1='$2'\" >> ~/.bash_profile";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top