I'm trying to set an environmental variable that will persist once the script has finished running. It can go away after I end an ssh session.
Sample bash script:
# User picks an option 1) export dogs = cool 2) export dogs = not_cool Running the script as source script.sh doesn't work since it kicks me out of my shell when ran and also requires the interactive menu so it won't work. Basically I want the user to be able to pick an option to switch between environmental variables in their shell. Is that even possible?
Source:
#!/bin/bash set -x show_menu(){ NORMAL=`echo "\033[m"` MENU=`echo "\033[36m"` #Blue NUMBER=`echo "\033[33m"` #yellow FGRED=`echo "\033[41m"` RED_TEXT=`echo "\033[31m"` ENTER_LINE=`echo "\033[33m"` echo -e "${MENU}*********************************************${NORMAL}" echo -e "${MENU}**${NUMBER} 1)${MENU} Option 1 ${NORMAL}" echo -e "${MENU}**${NUMBER} 2)${MENU} Option 2 ${NORMAL}" echo -e "${MENU}*********************************************${NORMAL}" echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}" read opt } function option_picked() { COLOR='\033[01;31m' # bold red RESET='\033[00;00m' # normal white MESSAGE=${@:-"${RESET}Error: No message passed"} echo -e "${COLOR}${MESSAGE}${RESET}" } clear show_menu while [ opt != '' ] do if [[ $opt = "" ]]; then exit; else case $opt in 1) clear; option_picked "Option 1"; export dogs=cool show_menu; ;; 2) clear; option_picked "Option 2"; export dogs=not_cool show_menu; ;; x)exit; ;; \n)exit; ;; *)clear; option_picked "Pick an option from the menu"; show_menu; ;; esac fi done
bashscriptscript.shto usereturninstead ofexitso that it doesn't end your current shell. Please post your shell script, or at least the relevant part(s).source. That's ok. The question is: where is the script? ;)returncommand instead ofexitso that the shell itself doesn't exit prematurely.export dogs=cool