Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, I have a simple bash file

cd ~/hello

How can I make it display every command before executing it? Just the opposite effect of "@echo off" in windows batch scripting.

share|improve this question
For those looking for what the set options are, you can find them on this man page. – mkobit Oct 16 '15 at 16:41
You can simply extend shebang like this #!/bin/bash -x – sobi3ch Mar 22 at 10:48
up vote 75 down vote accepted
bash -x script


set -x

in the script.

You can unset the option again with set +x. If you just want to do it for a few commands you can use a subshell: `(set -x; command1; command; ...;)

share|improve this answer
I have always used this to great effect. The output looks a little dirtier than you might first expect. – Scott Pack May 31 '09 at 13:51
By setting PS4 you can tweak the prompt of -x. E.g.: PS4='Line $LINENO @ $(date +%s.%N): ' bash -x script (date will slow it down, though) – Walter Tross Nov 17 '14 at 11:15

These also work:

set -v


#!/bin/bash -v

But -v doesn't print the PS4 string before each script line and it doesn't trace the steps of a "for" statement (for example) individually. It does echo comments while -x doesn't.

Here's an example of the output using -v:

#!/bin/bash -v
# this is a comment
for i in {1..4}
    echo -n $i

echo hello

Here's the result of the same script with -x:

+ for i in '{1..4}'
+ echo -n 1
1+ for i in '{1..4}'
+ echo -n 2
2+ for i in '{1..4}'
+ echo -n 3
3+ for i in '{1..4}'
+ echo -n 4
4+ echo

+ echo hello

Note that I included "echo -n" to add emphasis to the differences between -v and -x. Also, -v is the same as "-o verbose", but the latter seems not to work as part of a shebang.

share|improve this answer
Great explanation! without doing any research, it looks to me like Verbose and eXplicit are what those two letters stand for. – Ape-inago May 31 '09 at 15:58
I think -x is closer to eXecute. – Steven Parkes Aug 25 '12 at 15:08

This should also work:

#!/bin/bash -x
cd ~/hello
share|improve this answer

This should work:

set -o verbose #echo on
set +o verbose #echo off
share|improve this answer

There are a few ways.

#!/usr/bin/env bash -x

as the shebang line.

Including set -x in the script itself will enable the functionality while set +x will disable it. Both of these methods will also work with the more portable sh shell.

If I remember correctly perl also has the -x option.

share|improve this answer

goes like

language -x script

language = python, perl, bash -x = operator script = filename

hope it helps.

share|improve this answer
-x has nothing to with echoing in Python – Hardex May 26 '15 at 14:50

#! /bin/bash -x does what you want.

share|improve this answer

set -o xtrace and set +o xtrace are your friends (it is more verbose, than -o verbose, and the output goes to STDERR, as opposed to verbose, which seems to log to STDOUT).

See more tips here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.