What does `set -x` do?

前端 未结 3 2045
温柔的废话
温柔的废话 2020-11-29 16:26

I have a shell script with the following line in it:

[ \"$DEBUG\" == \'true\' ] && set -x
3条回答
  •  温柔的废话
    2020-11-29 17:08

    set -x

    Prints a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

    [source]

    Example

    set -x
    echo `expr 10 + 20 `
    + expr 10 + 20
    + echo 30
    30
    

    set +x
    echo `expr 10 + 20 `
    30
    

    Above example illustrates the usage of set -x. When it is used, above arithmetic expression has been expanded. We could see how a singe line has been evaluated step by step.

    • First step expr has been evaluated.
    • Second step echo has been evaluated.

    To know more about set → visit this link

    when it comes to your shell script,

    [ "$DEBUG" == 'true' ] && set -x
    

    Your script might have been printing some additional lines of information when the execution mode selected as DEBUG. Traditionally people used to enable debug mode when a script called with optional argument such as -d

提交回复
热议问题