简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM