0

Is it possible to echo out what command is being run in a shell script?

(Without put the -X in the sheabang to turn on the debuggin mode. This is not part of a debugging process)

thanks

[Edit: more details]

Suppose I have a script like this:

#! /bin/sh

clear
cd ~/home/

How do I have the two commands that are executed (cd and clear) printed to the console?

4
  • 3
    No. This is exactly what -x is for. Commented May 23, 2012 at 21:03
  • 1
    If you don't want to use -x, consider -v instead. Commented May 23, 2012 at 23:02
  • @larsmans: I would've marked your comment as the answer :), but didn't know how to. Commented May 24, 2012 at 18:26
  • @OneTwoThree: posted it as an answer. Commented May 24, 2012 at 19:27

2 Answers 2

1

On my version of /bin/sh (the Debian Almquist Shell, dash), there's no other way than -x to get this behavior. Echoing commands is also the only effect of -x, so I see no reason not to use it.

Sign up to request clarification or add additional context in comments.

Comments

0
echo $0

That will echo the first word in the command line. If you specified a relative path, such as "./bin/test", that will show the whole path, so you may want:

echo $(basename $0)

That will just echo "test", instead of "./bin/test".

It's very common for scripts to use $0 in error messages, rather than hard coding the name, because the program could be renamed or called from a symlink.

5 Comments

All good info. But I think the O.P. is looking to echo that a cmd is running further down in the script. I.E. #!/bin/bash;cmd1; echo cmd2; cmd2; cmd3. Good luck to all.
Uhm, I didn't mean to print out the name of the script ($0).
"echo $0" does exactly what you asked: echoes the command being run. What do you want? To echo the command that's going to be run? Echo the command being run in another process?
I don't think you understand what I asked for. (I've editted the question with more details)
I guess I just need more context of why you'd want to do this outside of a debugging context. You could create a list of commands and arguments and iterate over it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.