Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

bash -x command

I saw a client doing $bash -x command to see if the file is executable (or ksh -x command, etc.) like the -x in the if statement in the shell script.

My question is: What does $bash -x command do?

My interpretation was to start a command in a new bash shell within the current shell, inheriting the same environment variables and executed by the same user.

The funny thing is I can do $ls but not $bash -x ls, which give:

(under AIX 6) /usr/bin/ls: /usr/bin/ls: cannot execute binary file

It is a mystery for me why the error is - guessing it is due to a privilege which means my assumption above is not correct.

Also, I believe $bash ls and $bash -x ls is the same thing (-x for "execute")?

Any comments are greatly appreciated.

Cheers!

question from:https://stackoverflow.com/questions/10107124/bash-x-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The -x option starts a BASH shell in tracing mode. You can see all the details of how your command/script is processed. It's a good way to find some bugs if your script does not do what you would expect to

And, just as Alex said in a comment, to run a command in BASH, you have to use -c option like bash -x -c ls.

See man bash or the online manual, specifically the parts on invoking Bash and the set builtin command for more information:

All of the single-character options used with the set builtin (see The Set Builtin) can be used as options when the shell is invoked.

-x

Print 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...