0

I am using Centos 7 Here is my script

#!/bin/bash

echo "select option"
echo "1. test1"
echo "2. test2"
echo "3. test3"
echo "4. test4"

read option

case "$option" in
   "1") echo "sourcing test1"
        source ~/ansible-keys/test1-file.rc
        echo "sourced Espoo 42"
   ;;
   "2") echo "sourcing test2"
        source ~/ansible-keys/test2-file.rc
   ;;
   "3") echo "sourcing test3"
        source ~/ansible-keys/option3-file.rc
   ;;
   "4") echo "sourcing test4"
        source ~/ansible-keys/option4-file.rc
        break
   ;;
esac 

I read an option from the user input values (1 to 4) and then execute the corresponding case. The .rc files basically exports a few variables using export command. But when I execute this script the source commands seems not working.

I tried to replace, source with . . But it was not working too.

How to fix this?

Arun Mohan
  • 898
  • 3
  • 18
  • 37
  • 2
    You are executing the script itself, so it doesn't matter that you use `source` inside; those `source` commands are affecting an environment that is discarded after your script process exits. You need to source the script as well. – chepner Aug 16 '21 at 18:58
  • This might help: [How to debug a bash script?](http://unix.stackexchange.com/q/155551/74329) – Cyrus Aug 16 '21 at 18:59
  • "Seems not to be working" is not specific enough to be actionable. We need a [mre] -- the shortest possible thing that can be run without changes. That said, chepner's diagnosis is certainly correct if you expect variables set inside the `source`d files to be present in the parent shell (the one that started the script, as opposed to the one that's _running_ the script). – Charles Duffy Aug 16 '21 at 19:00
  • `export` only exports variables to child processes, not the parent process. – Barmar Aug 16 '21 at 19:01

0 Answers0