1

test.sh contains:

A=$1
B=$2

I set test.sh to chmod 777

I started the script with 2 parameters:

./test.sh first last

Then I tested it by typing:

echo "FirstVar: $A SecondVar: $B"

Result:

"FirstVar:  SecondVar: "

What did I do wrong?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Lara
  • 84
  • 1
  • 7
  • 30
  • 1
    You didn't do anything wrong. Your code worked just like it was supposed to (setting variables valid *in the scope of the interpreter running it*). – Charles Duffy Feb 23 '16 at 19:35
  • 1
    Whatever you are trying to accomplish, making your executables writable by everyone with access to the system is a **serious security problem.** Maybe try `chmod 755` instead. – tripleee Feb 23 '16 at 19:41

1 Answers1

2

When you run your script like this:

./test.sh whatever

Bash runs another instance of shell which interprets command in your script. If you want to set variables in the current shell, you should use source command, or dot (.). In this case the commands in the script will be executed in the current shell directly.

source test.sh

or just

. test.sh
Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36