9

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?

jlliagre
  • 61,204
user3871
  • 241
  • Do you want the backtick evaluated at the time the PS1 is set, or each time the prompt is displayed? – Stephen Rauch Jan 02 '17 at 22:01
  • You meant to use tee -a, right? – Kusalananda Jan 02 '17 at 22:18
  • @Kusalananda in the PS1 script above, I understand it to be: [$?=0] means IF Exit Code is 0, aka successful and X=2 OR X=1, THEN output color code $X. Two questions: 1) What is X=2 || X =1? What is X here? 2), In tput setaf $X, what is $X and why do we set the color code based on this? – user3871 Jan 02 '17 at 22:27
  • @StephenRauch what do you mean? The backtick is going to be eval'ed each time a command is run such that I can change the color of $localhost [username] to red or green depending on status code – user3871 Jan 02 '17 at 22:31
  • @Growler Yes, but the tee without the -a will overwrite the existing .bashrc. With -a it will append. I'm not really interested in what you do inside the here-document at all :-) – Kusalananda Jan 02 '17 at 22:32
  • @Kusalananda yes I originally had it overwrite on purpose as there wasn't anything else in the documents. This is a script for initial setup of Mac. Can you answer my comment above? I had 2 questions – user3871 Jan 02 '17 at 22:33
  • @Growler 1) If $? is zero, X gets 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 1 and tput setaf 2). Why you'd want to do that is not up to me to answer. – Kusalananda Jan 02 '17 at 22:41

1 Answers1

13

In the here-document, as you've written it, the shell will do parameter expansion ($PWD will be expanded to the path of the current directory at the time of parsing the script, for example), command substitution (the "backticks" will be executed and whatever that command produces will be inserted in their place, at the time of parsing the here-document) and arithmetic expansion (none of that in your case).

If you don't want this, i.e. you want the contents of the here-document to be delivered as-is, then you can do this:

tee -a "$HOME/.bashrc" <<'EOF'

# contents of here-document here

EOF

The apostrophes around the EOF on the first line prevents the shell from expanding anything in the here-document itself.

Kusalananda
  • 333,661
  • Why use $HOME if ~/ represents home – user3871 Jan 02 '17 at 22:56
  • @Growler I tend to use $HOME rather than ~, even on the command line. I think it looks nicer, and also because it behaves like variables should behave. See also http://unix.stackexchange.com/a/146697/116858 – Kusalananda Jan 02 '17 at 23:02
  • Any thoughts as to why my question was down voted? – user3871 Jan 02 '17 at 23:47
  • @Growler No, I don't have any thoughts on that, sorry. In general, don't ask about why a question/answer was downvoted, but rather how it may be improved (for example on the meta forum). If you had asked me "How do I ask a good question?", I would have directed you to http://unix.stackexchange.com/help/how-to-ask I will not answer further meta-questions, sorry. – Kusalananda Jan 03 '17 at 00:04
  • Behaving "like variables should behave" includes word-splitting, which you would never want for HOME... – Michael Homer Jan 03 '17 at 00:24
  • @MichaelHomer And that's why I added double quotes around it right now. Thanks for the reminder! – Kusalananda Jan 03 '17 at 06:13