Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Prompt from Shell Script? [duplicate]

Tags:

bash

shell

ps1

I'm new to shell scripting and can't for the life of me figure out why this isn't working.

I'm trying to change the prompt from inside my shell script. It works when I type it into the terminal, but does nothing when I run the script and choose it from the menu. Here's what I have:

read input   
case $input in   
1)    oldprompt=$PS1  
export PS1="\d \t"    
;;  
2) echo "option 2"  
;;  
*) echo "option 3"  
;;   
esac   
like image 606
PSherman Avatar asked Mar 09 '16 14:03

PSherman


1 Answers

environment variables are local to a process and only propagate down to its children. if you execute a script and it exports variables, that, by design, has no impact on the parent process.

instead, you need to source the shell script so it executes in the current context.

# This is wrong.
$ ./myscript.sh
# This will work though.
$ . ./myscript.sh
like image 165
Mike Frysinger Avatar answered Sep 20 '22 22:09

Mike Frysinger