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

Run batch (.bat) files in CMD

Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go

Changing CMD text color using batch script

Publish Date:2025/03/20 Views:171 Category:OPERATING SYSTEM

This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr

Batch set command timeout

Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM

This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a

Batch merge XML files

Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM

This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku

Run .exe file from command prompt using batch script

Publish Date:2025/03/20 Views:76 Category:OPERATING SYSTEM

This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an

Deleting files older than N days using batch script

Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM

In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -

Deleting files using batch script

Publish Date:2025/03/20 Views:178 Category:OPERATING SYSTEM

This article will demonstrate how to delete files using a batch script. Deleting files using batch script Generally, we can easily delete files by clicking on delete or pressing the delete button on the keyboard. But in Batch, we have to fo

Concatenate multiple files using batch script

Publish Date:2025/03/20 Views:195 Category:OPERATING SYSTEM

In this article, we will see how to concatenate multiple files into one file. Concatenate multiple files using batch script The general format of the commands we will use is shown below. type FileONE.txt FileTWO.txt ConcatFile.txt There are

Check if a file exists using batch processing

Publish Date:2025/03/20 Views:100 Category:OPERATING SYSTEM

This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial