Score:1

How do I change the command prompt using a bash script?

do flag

My Ubuntu version is Ubuntu 21.04. I am trying to change my command prompt with a script I made, called recording.sh. Here is the code:

#!/bin/bash  

echo -n "are you recording? yes(1)/no(0)"  
read VAR  

if [ $VAR -eq 1 ]; then  
       PS1="\[\033[38;5;2m\]Hazardous-potato@\h\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;4m\]\w\[$(tput sgr0)\]: \[$(tput sgr0)\]"  
else  
       PS1="\[\033[38;5;2m\]\u@\h\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;4m\]\w\[$(tput sgr0)\]: \[$(tput sgr0)\]"  
fi

I expect my command prompt to change from:

sterling@The-beast~:

to

Hazardous-potato@The-beast~:

when I run the above script, but, instead, it just stays the same.

ar flag
When you change a variable in a bash file it only changes for the duration of the bash file. It has no effect once the file has run.
Sterling Russell avatar
do flag
is there a way to run it until the terminal closes?
hr flag
To take effect in the parent interactive shell you would need to *source* the file rather than run it
Sterling Russell avatar
do flag
how do I do that?
ar flag
@steeldriver that's the answer! prefix the bash script with `source`. I have seen the `source` used before, now it is clear to me what it does.
Score:0
do flag

I sourced the file and lead bash into the file to execute it when the terminal opens. I sourced it at the end of the .bashrc file:

. /home/sterling/recording.sh
ar flag
You don't need to put it in the `.bashrc` file if you don't want to run it every time you open a terminal. You can run it when you want in any terminal by typing `. ./recording.sh`. Note two dots separated by a space in the front before the `/`. Alternately you can type `source ./recording.sh`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.