0

Hello StackExchange community,
This post relates to su options - running command as another user (however I cannot make comments yet, as I have created a profile to ask this specific question below).

I would like to ask the following question (as Google cannot find the answer for me).

I have a command, which is the following:
sudo su - usertwo -c 'mv /home/usertwo/file1.txt /home/usertwo/file2.txt'

The above command is executed by userone.
I would like the command to also appear in .bash_history of usertwo.

Is there a way I can "forward" the entry in .bash_history of userone into usertwo?

Thanks in advance!

Dan W
  • 1

1 Answers1

0

A better command is

sudo -Hu usertwo mv file1.txt file2.txt

But in either this case or yours the command isn't run interactively as usertwo so it won't go into that user account's .bash_history.

You can create a (horrible) workaround, though, by forcibly enabling history tracking:

sudo -Hu usertwo bash -c '
    set -o history; HISTFILE="$HOME/.bash_history"; history -n
    mv file1.txt file2.txt
'
Chris Davies
  • 116,213
  • 16
  • 160
  • 287