0

Attempting to enable logging of bash commands to syslog, including the ip of the logged in ssh user. The end goal is to have a script which will echo 'export PROMPT_COMMAND=....'' >> ~/.bashrc

This works if I manually edit .bashrc:

export PROMPT_COMMAND='trap "" 1 2 15; history -a >(tee -a ~/.bash_history | while read line; do if [[ $line =~ ^#[0-9]*$ ]]; then continue; fi; logger -p local3.debug -t "bash[$$]" "($USER $SSH_CONNECTION) $line"; done); trap 1 2 15;'

Adding awk '{print $1}' causes problems, I suspect because of quotes.

This doesn't work:

export PROMPT_COMMAND='trap "" 1 2 15; history -a >(tee -a ~/.bash_history | while read line; do if [[ $line =~ ^#[0-9]*$ ]]; then continue; fi; logger -p local3.debug -t "bash[$$]" "($USER) $(echo $SSH_CONNECTION | awk '{print $1}') $line"; done); trap 1 2 15;'

Attempting to wrap these quotes in the echo 'export PROMPT_COMMAND=....'' >> ~/.bashrc is also giving me a headache.

Any suggestions on both of these issues?

  • 2
    Why don't you write a shell function and call that from PROMPT_COMMAND? Why do you export PROMPT_COMMAND? – Kusalananda Jan 27 at 8:10
  • @Kusalananda, I attempted to create a function as this seemed like the correct way to do this. Unfortunately ended up breaking bashrc and temporarily locking myself out of the VM. The single line command seemed easier. – Alex R Jan 27 at 18:18
  • Well, in that case, don't put single quotes inside a single quoted string (see the awk bit). Doing it as a function would allow you to write readable code that doesn't have to use quoting tricks. It would also allow you to test it outside of PROMPT_COMMAND before even setting that variable. – Kusalananda Jan 27 at 18:29

Your Answer

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

Browse other questions tagged or ask your own question.