Bash Set in Fish
In this article, we will learn to bash
set -u and
set -x in the Fish shell.
set -x command is used for debugging Bash scripts by printing each executed statement to the shell for debugging and troubleshooting purposes. The
set -x command may be run directly in an interactive Bash shell or within a Bash script.
set -x command typically places the Bash shell’s initialization after the
We enable debugging in this example using the
set -x command. By default, debugging is disabled in Bash.
Instead, each statement is printed to the Bash terminal, followed by the statement’s results, to facilitate the verification. For instance, the
echo "The First Tree" is written first, followed by its result.
Additionally, debugged statements begin with a plus sign.
#!/bin/bash set -x echo " The First Tree" echo "The Second Tree"
+ echo ' The First Tree' The First Tree + echo 'The Second Tree' The Second Tree
set -e command can be used to stop the execution of a script or function and return a non-zero error code if we rely on the script’s or function’s execution and want precise information about the result.
By default, Bash functions and scripts return 0 if their execution completes correctly and without error. However, if an issue occurs, an error code offers specific details about the error.
In the following example, we configure exit if an error occurs at the beginning of the script. As there is no root permission,
mkdir /root/test returns an error, and the directory cannot be created.
Consequently, the line
echo "Progress..." can now be run and shown on the terminal.
#!/bin/bash set -e mkdir /root/test echo "Progress..."
set-u command instructs the Bash shell to interpret unset variables as errors and quit immediately. This takes us considerably closer to the behavior of advanced languages.
set -u command can prevent and display errors for unused Bash variables.
#!/bin/bash firstName="Jackie" fullName="$firstname John" echo "$fullName"
Please refer to the preceding example. Do you see the mistake?
On the right side of the third line, the word
"firstname" appears in all lowercase instead of camel-cased. Without the
-u option, this error wouldn’t show.
However, with the
-u option, the script terminates on that line with an exit code of
1, and the message
"firstname: unbound variable" is printed to
Generally, we want it to fail explicitly and promptly instead of creating subtle bugs that may not be found until much later.
Command for Fish Shell
set fish_trace 1 # display the expanded command line before execution set fish_trace 2 # prepend file and line number to the command line set fish_trace 1 1 # print the command line before execution and the exit status following execution. set fish_trace 1 2 # as above, except print the exit status of all subprocesses (?) set fish_trace 0 1 # print command-line exit status
These options, such as
set -e, and
set -x, do not exist in Fish. There is no dedicated syntax for setting variables in Fish.
Instead, it uses a standard command:
set, which accepts a variable name and its value. If both a variable and values are supplied, the
set assigns the values to the variable with the specified name.
Multiple values are permitted since all variables in Fish are lists.