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?

2
  • 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). Commented Feb 23, 2016 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. Commented Feb 23, 2016 at 19:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.