Vanson Samuel
Vanson Samuel

Reputation: 2099

bash programming STDIN redirect

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

Upvotes: 0

Views: 321

Answers (3)

phatfingers
phatfingers

Reputation: 10250

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

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

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?

Upvotes: 0

Ariel
Ariel

Reputation: 404

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}'

Upvotes: 0

Related Questions