Is there a way for running a shell script with echoing the commands but w/o actually executing them?
Let's say I have script removing a file whose name is stored in a variable:
#!/bin/bash
set -v
FN="filename"
rm -f ${FN}
Adding set -v
will echo the following commands before execution:
$ ./scr.sh
FN="filename"
rm -f ${FN}
Now, I want to see the flow of this script w/o actually removing the file. IOW, I want to prevent any effect on the external environment and filesystem. Is this possible?
I could wrap all commands with an echo
command, but it is tiring for a long script.
Best Answer
There's no way to step through a script to see how it would execute without actually doing that. In your example, there are no
if
statements or loops. But in real scripts, there are often lots of conditional statements. Which branch will get taken will often depend on what happened when the shell ran the previous command. If it doesn't run the command, there's no way for the shell to know what output it would have generated or what the return code would have been, which the next conditional branch or assignment statement might depend on.If the point is that you'd like to scrutinize a script and know what it does before you run it, that's not a bad idea. But realistically, about the best way to do that is by just browsing the file with
less
orvi
or something similar.Added
If you're developing a script and you're wanting to step through it the first time, testing the logic but not actually doing any damage if you've got a bug, the solution I'm often able to use is to modify only the statements that could do any damage by pasting an
echo
onto the front.This often works in typical real-life scripts because (a) generating the lists of items you'll iterate over or the value to which you'll set a variable can often be generated without changing the filesystem and (b) it's usually sufficient to assume that if you did run the command, it would succeed.