Katie
Katie

Reputation: 3707

Exporting environment variable in bash script - it doesnt work

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

Upvotes: 0

Views: 308

Answers (1)

chepner
chepner

Reputation: 532418

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.

Upvotes: 8

Related Questions