-1

I am using this script:

#!/bin/bash


case "$1" in

  start)
    echo "Iniciando servicio"




   export http_proxy=socks5://192.168.1.2:9090

   export https_proxy     <----I don't want it exported



    ;;
  stop)
    echo "Deteniendo servicio"

    ;;
  *)
   echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

but when executing it and by env I don't get the variable that I created :(

root@:~# /etc/init.d/a start
Iniciando servicio
root@:~# env
SHELL=/bin/bash
NO_AT_BRIDGE=1
PWD=/root
LOGNAME=root
LS_OPTIONS=--color=auto
HOME=/root
TERM=xterm
USER=root
SHLVL=1
SSH_CLIENT=
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
SSH_TTY=/dev/pts/0
TEXTDOMAIN=Linux-PAM
_=/usr/bin/env

I tried running the script with source but it doesn't work because I want to export a single variable not all

ortiga
  • 131
  • 7

1 Answers1

3

When you execute a script, it starts a new bash process. When the script ends, that process closes, taking the environment variable with it.

You need to source the script instead of executing it:

source /etc/init.d/a start

Also, you don't get any output from the export command so this

   export http_proxy=socks5://192.168.1.2:9090 >> ~/.bashrc

does not add anything to the .bashrc.

glenn jackman
  • 85,964
  • it works but another way to do it without having to execute the command with source ??? – ortiga Nov 07 '19 at 15:49
  • No. This is fundamental to the security of Unix : a child process cannot alter the environment of the parent. – glenn jackman Nov 07 '19 at 16:54
  • and if I had another variable for example export http = 1 as I would do so that that one did not export that specific one because with source all exports are exported – ortiga Nov 07 '19 at 16:56