In shell scripts I would like to echo some of the major (long running) commands for status and debug reason. I know I can enable an echo for all commands with set -x
or set -v
. But I don't want to see all the commands (specially not the echo commands). Is there a way to turn on the echo for just one command?
I could do like this, but that's ugly and echoes the line set +x
as well:
#!/bin/sh dir=/tmp echo List $dir set -x ls $dir set +x echo Done!
Is there a better way to do this?
The echo command writes text to standard output (stdout). The syntax of using the echo command is pretty straightforward: echo [OPTIONS] STRING... Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.
The 2 options are -n and -e . -n will not output the trailing newline. So that saves me from going to a new line each time I echo something. -e will allow me to interpret backslash escape symbols.
Explanation: echo will print the command but not execute it. Just omit it to actually execute the command.
echo command in linux is used to display line of text/string that are passed as an argument . This is a built in command that is mostly used in shell scripts and batch files to output status text to the screen or a file.
At the cost of a process per occasion, you can use:
(set -x; ls $dir)
This runs the command in a sub-shell, so the set -x
only affects what's inside the parentheses. You don't need to code or see the set +x
. I use this when I need to do selective tracing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With