Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo some command lines in a shell script (echo on for single command)

Tags:

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?

like image 539
Den Avatar asked Oct 04 '12 00:10

Den


People also ask

How do you echo a command in shell script?

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.

How do you echo one line?

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.

Does echo execute command?

Explanation: echo will print the command but not execute it. Just omit it to actually execute the command.

Is echo a shell 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.


1 Answers

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.

like image 136
Jonathan Leffler Avatar answered Sep 20 '22 08:09

Jonathan Leffler