7

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

7
  • 4
    at the beginning of your shell script use #!/bin/sh -x This will output each execution Commented Sep 25, 2013 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. Commented Sep 25, 2013 at 20:31
  • yes, I want to output my commands except the echo command, sorry for the confusion Commented Sep 25, 2013 at 20:34
  • 1
    What you are trying to do seems non standard and issues prone. Commented Sep 25, 2013 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... Commented Sep 26, 2013 at 0:10

4 Answers 4

8

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 
Sign up to request clarification or add additional context in comments.

1 Comment

That's a nice application! Related to it, I have this in my .gitconfig, which allows me to list the git aliases via git alias: [alias] alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
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 

2 Comments

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).
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.
-1

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

Comments

-3

This does what you'd like :

#!/bin/bash echo "script started" echo "./some_command > output_file" ./some_command > output_file echo "script ended" 

4 Comments

I have thought about that, but what if I have hundreds of lines. that creates a maintenance problem
This is the straightforward way.
@rid, there's a way with eval but don't use it (security issues). KISS. 'eval' is a common misspelling of 'evil'. If eval is the answer, surely you are asking the wrong question. See mywiki.wooledge.org/BashFAQ/048
@sputnick, seems like there's no better solutions. I guess I'll take yours as the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.