2

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
Juddles
  • 747
  • 1
  • 7
  • 12
  • I can't see where this is a `bash` script – hek2mgl Nov 04 '13 at 18:31
  • 4
    You cannot affect the current environment by running a child process. It sounds more like you need to modify `script.sh` to use `return` instead of `exit` so that it doesn't end your current shell. Please post your shell script, or at least the relevant part(s). – chepner Nov 04 '13 at 18:37
  • 1
    @chepner he's using `source`. That's ok. The question is: where is the script? ;) – hek2mgl Nov 04 '13 at 18:42
  • 1
    I know; that's why I recommended that it may need to use the `return` command instead of `exit` so that the shell itself doesn't exit prematurely. – chepner Nov 04 '13 at 18:44
  • 1
    @Juddles, there must not be spaces around the equal sign in a variable assignment: `export dogs=cool` – glenn jackman Nov 04 '13 at 18:56
  • sorry thats a type, there are no spaces. – Juddles Nov 04 '13 at 19:00

3 Answers3

1

The problem here is that . ./script.sh or source ./script.sh cannot run an interactive menu style script like this one. No way that I'm aware of to set local environmental variables from a bash script like I am trying to do here.

Juddles
  • 747
  • 1
  • 7
  • 12
1

Redirect your normal echo's for user interaction to stderr (>&2)

echo the value that you want to have in your parent's environment to stdout (>&1)

if you changed your script that way you can call it like:

ENV_VAR=$( /path/to/your_script arg1 arg2 arg3 arg_whatever )

and now you have "exported" a variable to the "parent"

thom
  • 2,294
  • 12
  • 9
0

Try running your script with "./myscript.sh" which will use the current shell without invoking the new shell (still i doubt the hash bang might invokes the new shell).

Have a look here

Can also be solved with ~/.bashrc. The required environments can added in this file. If you need new shell with your own environment, you invoke "bash" with "bash --rcfile ".

Community
  • 1
  • 1
Parthiban
  • 2,130
  • 2
  • 14
  • 27