40

I am reading a bash script and it start with set -x. I googled around and I did not find any single manual which does describe all the flags in details specially -x.

My apologies if I sound like someone who did not do his research before asking the question here but I sincerely did not find any information on set -x. Any ideas?

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Lost
  • 507
  • 1
  • 4
  • 8
  • Can you give a link or post the script? – Pilot6 Aug 23 '19 at 16:37
  • https://stackoverflow.com/questions/36273665/what-does-set-x-do – ajgringo619 Aug 23 '19 at 16:38
  • It's in a private git repo that I am viewing. – Lost Aug 23 '19 at 16:38
  • 1
    https://stackoverflow.com/q/36273665/6942873 – Pilot6 Aug 23 '19 at 16:38
  • Wow been looking for it and you found it on SO itself. Now I don't know wha to do with the question itself. Should I remove it? Or you can post the answer and I can mark it. – Lost Aug 23 '19 at 16:41
  • Related (but not a duplicate): [How to debug bash script?](https://askubuntu.com/questions/21136/how-to-debug-bash-script) – Eliah Kagan Aug 23 '19 at 16:51
  • [How can I get help on terminal commands?](https://askubuntu.com/q/991946/507051) – dessert Aug 23 '19 at 17:32
  • My goodness: Why *Google* when there is `man bash`? Despite of that: https://unix.stackexchange.com/questions/409366/what-is-bash-x https://stackoverflow.com/questions/64110936/bash-x-or-set-x https://linuxtect.com/what-does-bash-set-x-do/ – U. Windl Feb 15 '23 at 23:31

1 Answers1

36

As help set says:

      -x  Print commands and their arguments as they are executed.

It works both in interactive and non-interactive shells, so you can try running set -x in an interactive shell to see the effect. Each command that is run is echoed to you first (with + signs in front of them to help you distinguish them from most regular output).

ek@Cord:~$ echo hello world
+ echo hello world
hello world

(If you see way more output than you expect an an interactive shell, your shell may be running commands to build your prompt. For example, you'll see about twenty additional lines if your prompt is set up to show information about the git repository you're navigated to, even if you're not actually in a repo now.)

To turn it off run set +x. Somewhat confusingly, with set, - enables a shell option and + disables it.

For more information, see 4.3.1 The Set Builtin in the Bash reference manual.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
  • In `man bash` it’s under [SHELL BUILTIN COMMANDS/set](http://manpages.ubuntu.com/manpages/bionic/en/man1/bash.1.html#shell%20builtin%20commands): “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.” – dessert Aug 23 '19 at 17:36