Software & Apps Linux Linux 'set' Command for Bash Environment Variables Prompt Bash to behave in certain ways by setting local variables with 'set' by Juergen Haas Writer Former Lifewire writer Juergen Haas is a software developer, data scientist, and a fan of the Linux operating system. our editorial process Juergen Haas Updated on February 13, 2020 Linux Switching from Windows Tweet Share Email The set command in Linux activates or clears specific flags or settings within the Bash shell environment. Other shells use different methods of setting local variables. Using the Set Command The set command lets you control certain flags and features in Bash to determine the behavior of your scripts. These controls help to ensure that your scripts follows the intended path and certain quirks in Bash don't turn into problems. Start with something simple. Say you want to disable Bash's default behavior of overwriting files the '-C' flag will prevent it. For example, executing: set -C configures Bash to not overwrite an existing file when output redirection using >, >&, and <> is redirected to a file. By default, Bash allows redirected output to overwrite existing files. As you can see, a message appears letting you know that the file cannot be overwritten. This one's especially useful when debugging your scripts, and you don't want to accidentally lose existing data. Next, you might want to disable automatic file name generation, more commonly known as "globbing." Globbing can be super useful in finding files, but it can also cause some serious problems if a rogue character ends up in the wrong place in your script. The '-f' flag disables it. set -f Then, you can try running something like: ls * Normally, you'd see a pile of files and folders dumped out on your screen, but now, you'll only receive a message letting you know that there's no such directory. When you're debugging a script, you probably don't want a partially functional script to keep on running, causing havoc or producing incorrect results. You can enable the '-e' flag to stop a script immediately when something goes wrong. set -e On a similar note, the set command also enables you to see which command in your script is being executed followed by the result. This lets you more accurately debug by giving you a real time readout of where your script is and what the result of each command is. This super useful functionality comes from the '-x' flag. set -x You can try it out with a simple while loop to see each iteration. x=10while [ $x -gt 0 ]; do x=$[ $x-1 ] echo $x sleep 2done Notice how it plays out. You'll get a direct print out of each line. Then, Bash will run the line, and give you a print out of the result, if any, and move on to the next line. This method can be a massive time saver when debugging your scripts. In case you've been following along, you've probably noticed that the changes brought about with the set command seem permanent. They're not. Use the plus sign(+) before any of the flags to disable them. set +x For more information on how the set command works and whet it can do, you can always run: set --help For more extensive advice about configuring built-in variables for Bash, review the Bash manual's instructions about set.