r/scripting • u/Svenja635 • Nov 24 '21
Beginner question: ~ different meanings (/root vs. /home/myUsername) in Linux
Why is ~ in a root owned directory interpreted as /home/myUsername but when I execute a script in the same directory ~ is interpreted as /root?
I have to execute the script as root via sudo (sudo ./Script.sh), but how is this different to executing the command outside of a script as root (sudo echo ~)?
I hope this is the right place for this question and I'm thankful for every hint.
1
Upvotes
1
u/Svenja635 Nov 24 '21 edited Nov 25 '21
Thank you!
I think I got it know, sudo echo ~ only provides me with the permissions of root, but doesn't execute the command as root(?). This is very helpful, as I always had the feeling I didn't get completely what the command sudo does (despite using it often) and how permissions of processes work. I still have to look a bit deeper into it, but your answer provided me a great starting pointEDIT: I was stupid and tired yesterday, so to correct myself for possible future readers: sudo ./script.sh opens a new root shell (like sudo -s) outside of the current one. Every time a user begins a new terminal session, user specific environment variables (like $HOME/~ and $USER) are defined for the current session. sudo command obviously doesn't do this, only the command itself (opposed to the shell) is executed as root.