Search code examples
bashvariables

transfer variables from a main script to a different shell started from the first script


Ok so I'm sorry if this is a no-brainer, if it is unclear, or if it is impossible but here is my code, I am trying to get the /usr/bin/expect line of code to inherit/get variables from the main script. Its not working because it was started with a different shell (I think) and so it's not loading the variables in. This is my first post so if I am doing anything wrong please kindly correct me ;)

Thanks in advance, --A\\/

#!/bin/bash

red="\x1b[0;31m"
nc="\x1b[0m"


####### commented out till scipt is finished
# if [ $EUID -ne 0 ]; then
# echo -e "${red}[!]${nc} | script must be run as root"
# exit
# fi

read -p '[?] | ip address of device: ' ip
read -p '[?] | nickname to set for this config: ' name
read -p '[?] | username to generate keys for: ' usr
read -p '[?] | password for that user: ' pw


export $usr
export $pw
export $ip
export $name


/usr/bin/expect -c 'spawn ssh $usr@$ip; expect"(yes/no)? "; send "yes\r"; expect "Password:"; send "$pw"; interact'

Solution

  • You have to 1. export them correctly, and 2. use $env(foo) to refer to them:

    #!/bin/bash
    var="World"
    export var   # No $
    expect -c 'spawn echo Hello $env(var); interact'