3

I have the following command, which I am trying to append to my ~/.bash_profile:

echo 'alias N="cd $(pwd) && source ./bin/activate && cd new"" 

I want it to return echo:

alias N="cd /Users/david/Desktop/django2 && source ./bin/activate && cd new" 

What would be the correct way to escape the pwd or quotes?

3 Answers 3

3

This should work for you:

echo "alias N='cd $(pwd) && source ./bin/activate && cd new'" 

However recommend you to use function instead of alias.

echo "N() { cd '$PWD' && source ./bin/activate && cd new; }" 

Note use of $PWD instead of command pwd

Sign up to request clarification or add additional context in comments.

Comments

2

To echo a literal " inside of a double-quoted string, use the escape character \:

echo "alias N=\"cd $(pwd) && source ./bin/activate && cd new\"" 

Outputs on my box:

alias N="cd /home/bishop/ && source ./bin/activate && cd new" 

However, your use case suggests to me a function:

go() { local where where=${1:-${PWD}} cd "${where}" && source ./bin/activate && cd new } 

Comments

2

Avoid excessive escaping with a here document:

cat >> ~/.bash_profile << EOF alias N="cd $(pwd) && source ./bin/activate && cd new" EOF 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.