47

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!

askewchan
  • 45,161
  • 17
  • 118
  • 134
Khiet
  • 872
  • 1
  • 10
  • 15
  • 6
    to execute a bash command with debug mode (-x) enabled, use `bash -x -c your_command` – Alex Apr 11 '12 at 13:44
  • Thanks Alex for the concise answer, bash -x -c ls does what you described. So, "$bash -x command to see if the file is executable" is the WRONG then :) Thanks a lot! – Khiet Apr 11 '12 at 13:55
  • @Khiet If you want to find out if a file is executable than just type `ls -l command` – user219882 Apr 11 '12 at 13:57
  • @Tomas, yeah the funny issue we are having at the moment is a binary with -rwxr-xr-x gives "cannot execute binary file". Checked owner/group, possible corruption but no luck so far. Anyway, not expecting to get my entire job sorted here :) Cheers! – Khiet Apr 11 '12 at 14:01
  • @KurzedMetal Exactly XD, will be careful. – Khiet Apr 11 '12 at 14:05
  • 3
    to check if a file is executable use `[ -x file_in_question ]` in bash which will return 0 if it is executable. You can otherwise check what type the file is by using `file file_in_question` to get a verbose description of the file contents – Alex Apr 11 '12 at 14:25

2 Answers2

67

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.

Community
  • 1
  • 1
user219882
  • 15,274
  • 23
  • 93
  • 138
  • Thanks Tomas, man for bash isn't installed on our VM but man page online does actually say it all ;( thanks for everyone for such a quick answer, much appreciated! – Khiet Apr 11 '12 at 13:58
  • @Khiet You're welcome. If you don't have man installed, type `man bash` into Google and the first link is usually the manual page :) – user219882 Apr 11 '12 at 14:00
9
  • bash -x <file> runs the script <file> with tracing of each command executed

  • test -x <file> tests whether <file> has execute permissions for the current user.

It appears that you have muddled these two.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103