I tried executing a set of queries from a shell script while capturing both input (SQL query) and output in the log file.

But the input query is not displayed along with output. Only the outputs are being captured in the log file. Is there a way to capture the input query along with the corresponding input?

I did set echo on but it still won't work.

  • Is the output being directed to STDERR instead of STDOUT? We need a little more info than what you've provided. The sql command would be a good start, the entire script would be even better. – slm Nov 7 '13 at 15:55

If your shell script is bash or sh you can try appending the -x switch to the shebang and then run the script. Typically used for debugging shell scripts it will print the next line/command before executing. So if you have the sample script below that logs to file logfile

#!/bin/bash -x
echo "Hello world!" >> logfile
echo "Second command!" >> logfile

And you run the script as ./script 2>> logfile the logfile would read as:

+ echo 'Hello world!'
Hello world!
+ echo "Second command!" 
Second command!
  • hi friend, my server is AIX and shell is KSH, issue is i can not use echo in between sql connection, i have 4-5 queries – Pallvi Mahajan Sapen Nov 7 '13 at 16:03
  • The -x switch should also work in ksh. According to the Linux man page (I haven't seen AIX in ages) at least. – HBruijn Nov 7 '13 at 16:08

Your SQL client probably offers an option to echo the query before displaying the results. For example, mysql has the -v option, which, when used once, does exactly that.


echo "select 1 from dual" | mysql -v --table --user=u --password=p --host=h


select 1 from dual

| 1 |
| 1 |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.