Introduction Show
The set command is a built-in Linux shell command that displays and sets the names and values of shell and Linux environment variables. On Unix-like operating systems, the set command functions within the Bourne shell (sh), C shell (csh), and Korn shell (ksh). In this tutorial, you will learn what the set command is and how to use it. Prerequisites
Linux set Command SyntaxThe general syntax for the set command is: set [options] [arguments]Options In the context of the set command, [options] are settings or flags that are set or unset in the Bash shell environment. Use it to influence the behavior of defined shell scripts and help execute the desired tasks.
Arguments [arguments] are positional parameters and they are assigned in order with the following parameters:
Not specifying any options or arguments causes the command to print all shell variables. Exit Values The set command has three exit values:
Linux set Command OptionsThe set command provides an extensive list of options that can be combined. Most options have a corresponding -o flag that can be used to invoke the option. The table below lists all options and their respective alternative form using the -o flag syntax.
This section lists examples of the most common uses of the set command. Using the set Command Without OptionsRunning the command without options or arguments outputs a list of all settings - the names and values of all shell variables and functions. Since the list is very long, you can scroll through it using the Page Up and Page Down keys. Following is an example of a partial set command output: Script DebuggingThe set command is especially handy when you are trying to debug your scripts. Use the -x option with the set command to see which command in your script is being executed after the result, allowing you to determine each command's result. The following example demonstrates how to debug scripts with set -x. Follow the steps below: 1. Run set -x: set -x2. Use your favorite text editor (we use the vi editor) to create a script. We created a simple loop that allows us to see the -x option effects: x=10 while [ $x -gt 0 ]; do x=$[ $x-1 ] echo $x sleep 2 done3. Make sure to chmod the script to make it executable. This step is always mandatory before running a script. The syntax is: chmod +x [script-name.sh]4. Execute the script. The syntax is: ./[script-name.sh]The output prints one line at a time, runs it, shows the result if there is one, and moves on to the next line. Another way to enable debugging is to place the -x flag on the shebang line of the script: #!/bin/bash -xScript ExportingAutomatically export any variable or function created using the -a option. Exporting variables or functions allows other subshells and scripts to use them. Enable script exporting by running the following command: set -aThe following example shows how script exporting works. Follow the steps below: 1. Create a new script using your editor of choice. For example: one=1 two=2 three=3 four=4 /bin/bashThe /bin/bash argument marks the start of a new shell. 2. Check if the script works in a new shell: echo $one $two $three $fourThe output proves that the function we created was exported and works even when starting a new shell. Exit When a Command FailsUse the -e option to instruct a script to exit if it encounters an error during execution. Stopping a partially functional script helps prevent issues or incorrect results. Create a script with the following contents to test this option: #!/bin/bash set -e cat nonexistingfile echo "The end"In the example above, the script encounters an error when trying to show the contents of nonexistingfile because that file doesn't exist, causing it to exit at that point. Thus, the final echocommand isn't executed. Note: You can use the cat command to show a file's contents in a terminal window. Prevent Data LossThe default Bash setting is to overwrite existing files. However, using the -C option configures Bash not to overwrite an existing file when output redirection using >, >&, or <> is redirected to that file. For example: Bash first allows us to overwrite the listing.txt file. However, after running the set -C command, Bash outputs a message stating that it cannot overwrite an existing file. Report Non-Existent VariablesThe default setting in Bash is to ignore non-existent variables and work with existing ones. Using the -u option prevents Bash from ignoring variables that don't exist and makes it report the issue. For example, the following script doesn't contain the set -u command and Bash ignores that $var2 isn't defined. #!/bin/bash var1="123" echo $var1 $var2The output contains only $var1. However, changing the script and adding the set -u command prevents Bash from ignoring the problem and reports it: #!/bin/bash set -u var1="123" echo $var1 $var2Set Positional ParametersThe set command can also assign values to positional parameters. A positional parameter is a shell variable whose value is referenced using the following syntax: $[N]The [N] value is a digit that denotes the position of the parameter. For example, $1 is the first positional parameter, $2 is the second parameter, etc. For example: set first second thirdRunning the command above sets first to correspond to the $1 positional parameter, second to $2, and third to $3. Check this with the echo command: echo $2Unset positional parameters by running: set --Split StringsUse the set command to split strings based on spaces into separate variables. For example, split the strings in a variable called myvar, which says "This is a test". myvar="This is a test" set -- $myvar echo $1 echo $2 echo $3 echo $4Use this option to extract and filter out information from the output of a command, similar to what the awk command does. Set allexport and notify FlagsThe -o allexport flag allows you to automatically export all subsequently defined variables, while the -o notify flag instructs the shell to print job completion messages right away. To set the flags, run: set -o allexport -o notifyIn the following example, we see that the shell notifies you upon background job completion since the script was exported: Note: If you want to run a process in the background, add the ampersand (&) symbol at the end of the command. Conclusion You now know what the set command is and how you can use it in Linux. Test out the different options to better understand the command and maximize your control of the Linux environment used by different packages. If Bash is an interesting topic for you, check out our comprehensive tutorial on Bash Functions: How to Use Them. Which Windows operating system did the command line terminal interface Show up?Windows 10 – a new era for the Windows command-line!
Make major improvements to the Windows Console.
What is the name of the feature that allows for a weather application to be placed on the Windows 7 desktop?What is the name of the feature that allows for a weather application to be placed on the Win7 Desktop? C: Desktop Gadgets. Which of the following is used by BitLocker to be able to encrypt a drive? B: TPM :: Trusted Platform Module.
Which of the following Windows command prompt commands can be used to list all directories files and subdirectories contained in the current directory?Explanation: In Windows Command Prompt, dir and dir *. * commands are used to list all directory's files and subdirectories contained in the current directory.
What feature of Windows is represented in the taskbar notification area by a small flag icon?The Windows Action Center, represented by the small flag icon in the Notification Area will briefly display a message balloon when there is a problem with your security programs and backup.
|