27

I have seen a couple of shell scripts with the following shebang:

#!/bin/bash -x -v 

However, man bash does not explain what these arguments -x and -v stand for, if they are belong to bash at all.

So what do those -x and -v (and other possible arguments) mean?

Alex
  • 5,700

2 Answers2

36

From man bash (yes, it's a big man page, usually Google search is faster):

-x After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

Effectively: when you run a script, it will show all actions done in that script. So all ifs, loops and commands run. Very useful for debugging.

-v Print shell input lines as they are read. When a script is run, it will print the entire script as it reads the file. When you use the shell interactively, it will show each command after you press enter.

The quotes above are from the explanation of the set builtin command in the man bash, which also explains that the options for set can also be passed as arguments (on the shebang line):

The options are off by default unless otherwise noted. Using + rather than - causes these options to be turned off. The options can also be specified as arguments to an invocation of the shell. The current set of options may be found in $-. The return status is always true unless an invalid option is encountered.

mtak
  • 1,294
  • Ok, so it is a simple argument to bash, and I just might have a different version without these arguments? – Alex Apr 11 '14 at 12:26
  • If you are talking about bash: I think all bash releases of the past decade will support these arguments. If you are talking about the man page: it's huge and you need to know what you're looking for, but it is there (at least on Ubuntu 13.04). Usually a Google search is quicker. – mtak Apr 11 '14 at 12:28
  • 8
    +1 "Google search is faster" -> /-x a couple of times will get you there in man (actually, it's less). You can repeat the last search with / + up arrow, but you have to page down to get any current search hit off the screen (/ is forward search, ? is backward). – goldilocks Apr 11 '14 at 12:54
  • 2
    @TAFKA'goldilocks' I guess it depends on your distro. In the Ubuntu 13.04 bash manual page the 43th hit is the right one. It's easier to search for the next hit in less by using n for forward search and N for backward search. – mtak Apr 11 '14 at 13:11
  • Whoops, in fact I was looking at test -x under CONDITIONAL EXPRESSIONS (the third hit), not set -x. -x in man bash is a pretty bad case so getting more specific helps: If you use /^\s+-x\b, (= start of line + whitespace + -x + word boundary) you get to the right one in 5 hits...it would have been 4 but for the fact x matches X...which leads me to a question I've been meaning to ask. Anyway, w/ most man pages the ^\s+-x\b pattern should work first try for switches. – goldilocks Apr 11 '14 at 13:34
4

The bash man page does hint that these options are explained further down, actually, but it's easily overlooked.

Therefore your problem should actually read: The OPTIONS section of the bash man page is incomplete. The answer would be to either duplicate them, or highlight the first section of the OPTIONS section:

OPTIONS
   All  of  the  single-character shell options documented in the descrip‐
   tion of the set builtin command can be used as options when  the  shell
   is invoked.  [ ... ]

Finally, to make this complete: they are standard options for any POSIX shell for showing the code of the script when read (-v) and when run (-x). Output will appear on stderr.