0

I want to get the home directory of a user of a unix system. Why doesn't the following work:

# sudo su $offender -c "bash -s < <(echo echo \$HOME)"
sh: Syntax error: redirection unexpected
4
  • Why doesn't sudo su $offender -c 'echo $HOME' work for you? Commented Apr 7, 2012 at 2:52
  • I get a "redirection unexpected" error Commented Apr 7, 2012 at 2:54
  • Fully expected. < < doesn't make much sense. What i'm asking is, why all the shell gymnastics? It'd seem you don't need all the redirection and echoing commands and such... Commented Apr 7, 2012 at 2:56
  • Guess we should ask...what OS is this? Is it one where sh isn't a link to bash? (On my RH/CentOS Linux boxes, sh is actually bash...probably why i'm not seeing the issue you're having.) Commented Apr 7, 2012 at 3:03

3 Answers 3

1

It works for me in Ubuntu and Fedora using sudo su $offender -c "echo \$HOME"

You could also gouge it from your /etc/passwd file like so:

grep "^$offender" /etc/passwd | cut -d':' -f6
Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes the obvious escapes me. :-/
0

Don't really know why it doesn't work, but here's a way to get the home directory that does not involve spawning a shell as that user:

getent passwd "${offender:?No Offending Account Given}" | awk -F':' 'NR==1{print $6}'

1 Comment

Or do sudo su ${offender} -c 'bash -c "echo $HOME"', or sudo su ${offender} -c "bash -s <<<'echo \$HOME'" if you insist on running a process as the user you want to get information for.
0

The direct problem is that sh does not recognize process substitution (the <(echo $HOME)) notation, even when it is a link to bash.

It seems like a rather brute force way to get the information. Doesn't:

eval echo ~$offender

work for you?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.