Bash logging: two questions

Anonymous swell.k at
Wed Jul 21 16:17:51 UTC 2010

jimbob palmer <jimbobpalmer at> writes:

> Hello,
> I would like to run a bash script but to log output and exit codes.
> Essentially I would like to run the script with bash -x, but for that
> output to the log to go to a file, and the normal output as from
> running a normal script to go to the terminal.

Dunno about bash but in zsh it's easy

  #! /usr/bin/env zsh
  PS4='+%i:%N:%?> '
  exec 2>trace.log
  set -x

  # here goes the main script
  echo foo=$foo, $bar

It should work in sh(1) except you'll not see exit values in prompt.

Seems like bash doesn't have tcsh-like features: `%?' and printexitvalue.
I guess you'll have to write your own wrapper to put `$?' into stderr
after each command.

> My second question is about history. Bash has a -h option to remember
> the location of commands as they are looked up. Is it possible for
> this to be recorded in the history? e.g. if I run ls, it would record
> /bin/ls to the bash history file.

If bash has smth like zshaddhistory() it'd be easy...

More information about the freebsd-questions mailing list