I need to add a cmd prompt modifier, PS1="..." within a shell script I am setting up. The issue is, I am using backtick to execute a command prior the main command:
sudo tee ~/.bashrc <<EOF
# Display absolute path in cmd line, cmds on new line
PS1='\[`[ $? = 0 ] && X=2 || X=1; tput setaf $X`\]\h [\u]\[`tput sgr0`\]:$PWD\n\$ '
EOF
cat ~/.bashrc gives:
# Display absolute path in cmd line, cmds on new line
PS1='\[\]\h [\u]\[\]:/Users/myuser/Library/Application Support/Sublime Text 3/Packages/User\n$ '
nano ~/bashrc gives:
PS1='\[^[[32m\]\h [\u]\[^[(B^[[m\]:/Users/myuser/Library/Application Support/Sublime Text 3/Packages/User\n$ '
This clearly means that everything in the backtick is not properly read. How can I escape the backtick or otherwise write line that includes a backtick to a file using shell?
tee -a, right? – Kusalananda Jan 02 '17 at 22:18[$?=0]meansIF Exit Code is 0, aka successfulandX=2 OR X=1, THEN output color code$X. Two questions:1)What is X=2 || X =1? What is X here?2), Intput setaf $X, what is$Xand why do we set the color code based on this? – user3871 Jan 02 '17 at 22:27$localhost [username]to red or green depending on status code – user3871 Jan 02 '17 at 22:31teewithout the-awill overwrite the existing.bashrc. With-ait will append. I'm not really interested in what you do inside the here-document at all :-) – Kusalananda Jan 02 '17 at 22:32$?is zero,Xgets the value 2, otherwise it gets the value 1. 2) If the previous command was successful, the color of the text is set to green (2), otherwise red (1) (test it and see:tput setaf 1andtput setaf 2). Why you'd want to do that is not up to me to answer. – Kusalananda Jan 02 '17 at 22:41