7

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   
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
PSherman
  • 71
  • 1
  • 3

1 Answers1

14

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
Mike Frysinger
  • 2,827
  • 1
  • 21
  • 26