Ubuntu – single line command to do `su`

passwordsuusername

I can do an su with su <username> and it asks for my password. Is there a password parameter for su such that i wont be prompted for a password?

e.g. su <username> -p <password>

Best Answer

If you want to write a script that runs as a different user something like this works, though it dows output the word "password" without a newline to standard out

su - username <<!
enterpasswordhere
enter commands to run as the new user
!

if you have a user named fred with a password of 1234 and want to get an ls of fred's home directory as fred, without the password string displayed, it would look like

su - fred <<! >/dev/null 2>&1
1234
whoami > /dev/tty
ls > /dev/tty
!