JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Using set in Bash Shell

Author:JIYIK Last Updated:2025/03/20 Views:

The Bash shell contains several useful built-in commands for manipulating the environment of the currently running shell session. The built-in setcommand provides the ability to view and change shell environment variables and options.

This tutorial discusses setthe scope of commands and how to effectively write Bash scripts to set good environment options.


set -xCommand tracing in Bash using

By default, running setthe command alone will return a list of currently set variables and their values, including environment variables such as the Bash executable location, version information, and PATH.

The use of this command is when you want to see the commands executed. If you are a programmer, this can help you debug Bash scripts to see if they fail on a specific command.

To do this, execute set -xor set -o xtrace, which turns on command tracing. While the command itself does not output anything, subsequent commands will be printed before being executed. Using brace expansion (a recent Bash feature) in the command is helpful.

If you have a command that deletes numbered TXT files, enabling setcommand tracing will show the brace expansion along with all TXT files in that directory.

user@linux:~$ set -x
user@linux:~$ cd /tmp
+ cd /tmp
user@linux:~$ # example of expansion (and comments do not print out in traces)
user@linux:~$ touch {1..5}.txt
+ touch 1.txt 2.txt 3.txt 4.txt 5.txt
user@linux:~$ rm -f *.txt
+ rm -f *.txt
user@linux:~$ # however, asterisk globs do not expand.

setOther useful options in Bash

setThe command provides a number of other mutually exclusive options for shell operations similar to command tracing, such as not executing the command, changing the command line to an Emacs-style editor, printing a traceback on error, and so on.

Remember, to turn an option on, you must use set -o <option_name>and set +o <option_name>to turn it off.

The full list of options is explained below, with information taken from the GNU manual for the set built-in.

  1. allexport- If you have a Bash script that sets environment variables, and you want those variables to be available to the current shell and any subsequent subshells, set -aor set -o allexportto cause those variables to be exported and available to those shell contexts.
  2. braceexpand- Brace expansion allows us to expand a constant set of a pair of numbers into a space-delimited string of ranges of those numbers. If you want braces to be evaluated at a lower level in the shell, turning this option off will disable brace expansion.
  3. Emacs- Enables emacs-based command-line editing for those familiar with it.
  4. errexit- If a command in a Bash script exits with a non-zero status code, the entire script will stop. You can prevent the script from exiting prematurely by ORing the failed command with a second command that returns a zero exit code.
  5. errtrace- If the program exits with a non-zero status code, a traceback of the failed command is printed.
  6. hashall- Tracks command locations as they are searched in the environment PATH. It caches command paths in one location for faster searching and execution.
  7. histexpand- Expand !characters in string as a feature for history replacement. If you !have problems using characters in string and don't do much history replacement work, you can safely turn this off.
  8. 历史- This is an important feature that allows you to maintain a list of previously executed commands.
  9. ignoreeof- The EOF character (provided by Ctrl-D) normally exits the Bash shell or a command that accepts input. Disabling this option causes EOF to be ignored, which may break some commands that only accept EOF.
  10. Monitor- Enable job control so that processes running in the background print out their exit code when they finish executing. Useful for checking background processes when they finish.
  11. noclobber- Prevent Bash redirection utilities from, for example, >&、<>truncating existing files. This is handy if you're dealing with real log files when testing and don't want to accidentally delete existing logs.
  12. noexec- Prints out commands but does not execute them. If used in a script, noexecyou can skip some commands and execute others by turning them on and off at certain points.
  13. noglob- Prevent glob expansion (ie *.txt).
  14. Notify- When monitorused with , notifyprints the background job status code immediately, rather than waiting for the next shell prompt.
  15. nounset- If the variable is not set and you try to read its value, an error code is returned.
  16. onecmd- Read and execute the next command and then exit.

If you want to read more options, see the manual above. Hopefully, these options can improve your Bash programming experience.

If you want to learn about these options again from the command line, try running help set. We also recommend the Linux Documentation Project's page for setmore information about the command.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial