It is helpful to have a log of the shell commands being executed. It is also a bonus to have the variables expanded and shown. Here're two options to do this.
We can use set -x
or set -o xtrace
in a shell script. This will expand variables and print '+' before the line. Use set +x
to turn off this behavior. Here's an example shell script named script.sh
:
#!/bin/shset -xls $PWD;echo 'Hello world';set +x;ls $PWD;
The main.php
function runs script.sh
and is not the focus of this Answer. However, here's a brief explanation for it:
script.sh
so that the script can have multiple lines for easier reading.script.sh
which echoes the output.PWD
variable, which is the current working directory.Hello world
.Let's execute the widget. What can we see?
There are two boxes. The red bordered box is the output of the set
command and the other box with no border is the output of the other commands.
No border box output:
ls $PWD
command lists the files in the current directory.Hello world
is the result of the echo Hello World
command.ls $PWD
command also lists the files in the current directory.Red border box output:
set -x
until set +x
are printed with a '+' prefix. The lines printed include:+ ls /usercode
. The $PWD
is expanded to /usercode
.+ echo Hello world
+ set +x
. Notice how the second ls
command that comes after this line is not printed.We can also use set -v
or set -o verbose
. However, that doesn't expand the variables. Use set +v
to turn off this behavior. We take the previous script and replace lines 2 and 6 with the new commands, as highlighted:
#!/bin/shset -v;ls $PWD;echo 'Hello world';set +v;ls $PWD;
Let's execute the widget. What can we see?
Again, there are two boxes. The red bordered box is the output of the set
command and the other box with no border is the output of the other commands.
No border box output:
ls $PWD
command lists the files in the current directory.Hello world
is the result of the echo Hello World
command.ls $PWD
command also lists the files in the current directory.Red border box output:
set -v
until set +v
are printed without a prefix. The lines printed include:ls $PWD;
. This time, the $PWD
is not expanded. echo 'Hello world';
set +v;
. Notice how the second ls
command that comes after this line is not printed.