Escape quote in alias

Writing by on Wednesday, 22 of October , 2014 at 5:25 pm

I wanted to create change the prompt to show the active git branch through an alias.
Changing PS1 as follows would do it as follows:

# Try this in bash to get the git prompt, but how do you alias this?
PS1='[\u@\h \W$(__git_ps1 ” (%s)”)]\$

However I needed to create an alias for it and turns out that if you wanted to escape a single quote you could do it using the following: ‘”‘”‘

# Note here that the entire aliased string needs to be escaped using a quote
alias ingit=’PS1='”‘”‘[\u@\h \W$(__git_ps1 ” (%s)”)]\$'”‘””

Now I can activate the git prompt that I need through an alias that contains a combination of single and double quotes.

Now !!! I wanted to change the IP Address of my DHCP vm in the /etc/hosts file. (Related sed Article)

# something that works
$ sed -i ” ‘s/^192.168.*vm/192.168.1.12 vm/’ /etc/hosts

# i would like to do something like this
$ setvmip 192.168.1.12

# so the escaped version is
alias setvmip=’function _setvmip(){ sudo sed -i “” ‘”‘”‘s/^192.168.*vm/'”‘”‘$@'”‘”‘ vm/'”‘”‘ /etc/hosts ; }; _setvmip’

Leave a comment

Category: Linux

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.