Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
After using the "su test1" (but not "su - test1") command to switch to user test1, is there a way to get the home directory of test1?
I understand if "su - test1" is used then $HOME will be changed to the home directory of test1.
Thanks in advance!

You can't get the home directory without doing some shell scripting with the unix id command. The later versions of id command supports the -un option which provides the effective user id. However, my version of the id command doesn't support -un.
Executing the id command with no options displays this as an output:
1003(nails) gid=1000(people)
The following script parses the effective user id and then searchs the first field of the password file, /etc/passwd, and when a match is found, returns the sixth field:
#!/bin/ksh
# retrieve the user id from id command
uid=$(id|cut -d \( -f 2|cut -d \) -f 1)
while IFS=":" read puid f2 f3 f4 f5 homedir f7
do
if [[ "$uid" = "$puid" ]]
then # display the home directory
echo "$homedir"
break
fi
done < /etc/passwd

To get to home (home is a relative directory by the way) you use cd.
"By default, su does not change the current directory. It sets the environment variables `HOME' and `SHELL' from the password entry for USER, and if USER is not the super-user, sets `USER' and `LOGNAME' to USER. "
Could be way wrong. See man page for cd on your shell.See also pwd.
"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10

jefro:
Sorry I must disagree wtih you. At least in Solaris su <user id> doesn't change the user environment - including environmental variables. To change the environment, you must execute su - <user id>
According to the su MAN page:
If the first argument to su is a dash (-), the environment will be changed to what would be expected if the user actually logged in as the specified user.

OK,
"The following statements are true if the login shell is /usr/bin/sh or an empty string (which defaults to /usr/bin/sh) in the specific user's password file entry. If the first argument to su is a dash (-), the environment will be changed to what would be expected if the user actually logged in as the specified user. Otherwise, the environment is passed along, with the exception of $PATH, which is controlled by PATH and SUPATH in /etc/default/su."
"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |