简体   繁体   中英

what will bash do with an unset variable

I am confused by the behavior about how do bash treat a unset variable used in a shell command, like below:

rm -rf /$TO_BE_REMOVED

what will be done if i have not defined a variable TO_BE_REMOVED .

If you do that, the command executed will effectively try to remove / which is very, very bad. I mean, it will probably mostly fail (unless you're running as root), but still, it will be very bad.

You can avoid many of these sorts of bugs in Bash automatically with one simple command:

set -eu

If you put that at the top of your Bash script, the interpreter will stop and return an error code if your script ever invokes a command which returns an error which is not checked (that's the -e part), or if it uses an undefined variable (the -u part). This makes Bash considerably safer.

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