0

I wrote a little bash script to export environment variable:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath = /home/katie/Desktop

if [ -n "$path" ]; then
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
else
    export my_var=$path
fi

but I got error:

defaultPath: command not found

How to fix it?

WORKNG VERSION:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath=/home/user/Desktop

if [ -n "$path" ]; then
    export my_var=$path
else
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
fi

1 Answer 1

8

No whitespace is allowed surrounding the = in a variable assignment:

defaultPath=/home/katie/Desktop

With spaces, the line is interpreted as a simple command that attempts to execute the command defaultPath with two arguments, = and /home/katie/Desktop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It worked now but take a look how it worked: pastie.org/private/qlgozczj1nphfrdb1hvkw. Did I miss sth else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.