0

How does env variables work on bash when they are not set, in the example below, I wanted to delete a linux directory that i thought i have already set before but because i didn't set the variables, the operation is going to be executed on everything, i'd like some help to understand how does it work ? and how i can avoid that ?

     ubuntu@osn:~$ sudo rm -rf $I_HOME/$I_VERSION/
     rm: it is dangerous to operate recursively on ‘//’ (same as ‘/’)
     rm: use --no-preserve-root to override this failsafe
  • 1
    Related: [What's a concise way to check that environment variables are set in Unix shellscript](http://stackoverflow.com/questions/307503/whats-a-concise-way-to-check-that-environment-variables-are-set-in-unix-shellsc). You can just type `set -u` if you'd like it to always check for unset vars. – Mark Plotnick Apr 12 '17 at 19:12
  • `set -u` will always check for unset variables. If you only need those 2 variables checked you can use `rm -rf ${I_HOME:?Please set the value}/${I_VERSION:?What version is it again}/`. – alvits Apr 13 '17 at 01:38

1 Answers1

0

as you have not set the variables they're default value is '' so your statement becomes:

sudo rm -rf //

when you say this:

SOMEVAR="someValue"

it creates and sets the env variable for the current bash process. to set the variable for all processes created by bash you can write this:

export SOMEVAR="someValue"

you can also add the variable to all future bash processes by adding this line to your .bashrc file.

monkeyStix
  • 620
  • 5
  • 10