4

I'm writing a .sh script like the following:

echo script started
./some_command > output_file
echo script ended

I'd like the output to be the following:

script started   
./some_command > output_file   
script ended  

I've tried set -v, set +v but it didn't work properly,
is there anything similar to @echo in batch?

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Monster Hunter
  • 846
  • 1
  • 12
  • 24
  • 4
    at the beginning of your shell script use `#!/bin/sh -x` This will output each execution – CoderDake Sep 25 '13 at 20:29
  • I was going to suggest you use standard error like so: `echo script started >&2`, then run your script with `myscript 2> /dev/null`, but that does not seem to be what you want. – chepner Sep 25 '13 at 20:31
  • yes, I want to output my commands except the echo command, sorry for the confusion – Monster Hunter Sep 25 '13 at 20:34
  • 1
    What you are trying to do seems non standard and issues prone. – Gilles Quénot Sep 25 '13 at 20:55
  • 1
    @user1464870 Don't give up so fast and accept an answer you don't like... You just posted this 3 hours ago. Maybe change the title because what you really want to do seems to be print the commands within a bash script, except don't echo the echo command twice... – beroe Sep 26 '13 at 00:10
  • If you're running with `set -x`, does `: script started` do what you want? It prints that line to stderr (or wherever you have `BASH_XTRACEFD` directed), and doesn't generate an additional line with output. – Charles Duffy Nov 22 '17 at 00:47
  • btw, [`set -x` without it being printed](https://stackoverflow.com/questions/13195655/bash-set-x-without-it-being-printed) is very closely related, perhaps even duplicate. – Charles Duffy Nov 22 '17 at 00:50

4 Answers4

7

Using bash -x from the comment by @TopGunCoder:

#!/usr/bin/env bash -x
echo $0 started  > /dev/null
ls
pwd
./some_command > output_file
date
echo $0 ended  > /dev/null

The OUTPUT is each command followed by its result:

+ echo /Users/myuser/scripts/tbash.sh started
+ ls
[...my directory listing...]
+ pwd
/Users/myuser
+ ./some_command
[the output of "some command"]
+ date
Wed Sep 25 17:06:25 PDT 2013
+ echo /Users/myuser/scripts/tbash.sh ended
beroe
  • 11,784
  • 5
  • 34
  • 79
  • 1
    The command doesn't even need to be `echo`, then -- one could run `: "$0" started`, and have the same effect, but with a shorter prefix and no need for a `>/dev/null` redirection. (`:` is a synonym for `true`, but one conventionally/traditionally understood to indicate this kind of placeholder use). – Charles Duffy Nov 22 '17 at 00:55
  • Thanks, Charles. I didn't know about `:`. This was answered a long time ago, and I think I just adapted the OP's example. Not sure why I even put the redirection. – beroe Nov 22 '17 at 01:04
1

If you don't want to bash -x the whole file, then as long as you don't mind running in a subshell, you can simply do this per line:

(set -x; command ...)

Quick and easy to use. I also use this in my git config to echo git aliased commands so I remember what they're aliased to

~/.gitconfig
[alias]
    c    = "!f() { (set -x; git commit --verbose \"$@\"; ) }; f"
    c-   = "!f() { (set -x; git commit -m \"$@\"; ) }; f"
    dt   = "!f() { (set -x; git describe --long --tags;) }; f;"
    push = "!f() { (set -x; git push origin \"$@\";) }; f"
    s    = "!f() { (set -x; git status -sb \"$@\"; ) }; f"
    ...
$ git s
+ git status -sb
## main...origin/main
 M file-1
 M file-2
?? file-3
davfive
  • 323
  • 4
  • 13
-1

Instead of using echo just replace it with # that way echo isn't displayed and you still get some feedback

CoderDake
  • 1,497
  • 2
  • 15
  • 30
-3

This does what you'd like :

#!/bin/bash

echo "script started"
echo "./some_command > output_file"
./some_command > output_file
echo "script ended"
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223