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
or vi
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.
I'm not a bash-scripting expert, but if I would write this with another programming language, I would store all commands in an array. this way it should be easy to loop through and either execute the commands or print the out. – hugo der hungrige – 2014-03-16T20:34:54.780