0

I am attempting to create a script that shows how much memory every user on the system is using in their home directory. Everything was going smooth till I started using the du -sh command to start pulling information about specific users. I attempt to run:

du -sh ~currentUser
enter code here

I understand how the command works in command line. How do I get it to work while using a variable username? Below reguser is dummy account I have on my system I use to screw around with.

:/home/reguser/Documents# du -sh ~reguser

Gives output of

71M /home/reguser

4
  • I was able to use du "/home/$currentUser". But why can't i use the ~ to reference the home Dir in the command? Commented Jun 21, 2022 at 2:31
  • Don't add comments to your own question, use the EDIT button to add information and improve your question. Commented Jun 21, 2022 at 3:33
  • Since when does ~ expands to /home/? It's supposed to expand to the current value of the HOME env var. Commented Jun 23, 2022 at 0:50
  • @TomYan ~ alone expands to $HOME but ~username expands to homedir of that user from getpwnam (equivalent to getent passwd, as in glenn's answer) and nowadays distros usually default user homedirs to /home/$user Commented Jun 23, 2022 at 2:53

1 Answer 1

2

It's because, in the order of Shell Expansions, tilde expansion occurs before variable expansion.

Instead of assuming "/home" you can use

getent passwd $user | cut -d: -f6

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.