Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am wondering why the following script won't work:

#!/bin/bash
read -p 'First lets write PROMPT.SH'
echo '#!/bin/bash'>prompt.sh
echo "export PS1='\e[1;31m\]\u@\h \W\$\e[0m\] '">>prompt.sh
read -p "Change Title and Prompt"
source prompt.sh
echo texttexttext
read -p "Now lets tidy up PROMPT.SH"
rm ./prompt.sh
read -p "That was the program"

What I am trying to do is change the window title, prompt, and text color that I can normally do with,

PS1='\e[1;31m\]\u@\h \W\$\e[0m\] '

during a regular bash session but not when I add it to a script and then run the script.

share|improve this question

2 Answers 2

PS1 is only displayed for interactive prompts. It has nothing to do with the prompt shown for read -p. Execution of a bash script, even if reading input, is not considered interactive.

share|improve this answer

I’m not crystal clear on what you’re trying to do, but I guess it’s setting PS1 for your interactive shell from a script.  Stated like that, you cannot do it.  In *nix and Unix-like systems, process characteristics/ properties get passed from parent to child, but never the other way around.  For example, if you set your PATH to include /foo, and then you run a script that tries to run bar, it will find /foo/bar — but if you change PATH in a script, it doesn’t affect your interactive shell.  Likewise for cd and umask. These are differences from the Windows Command Prompt (batch) world.

That said, there is an answer, and you already know it.  If your script (the 10-line one) is called andy.sh, then you should be able to get the effect you want by typing source andy.sh.  But that’s silly overkill (unless you’re doing it just for script-writing practice); just put

PS1='\e[1;31m\]\u@\h \W\$\e[0m\] '

into drew.sh and type source drew.sh.

But also research the alias command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.