What's the difference between these Bash expressions?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
Dry Falls
Posts: 616
Joined: Tue 16 Dec 2014, 23:37
Location: Upper Columbia

What's the difference between these Bash expressions?

#1 Post by Dry Falls »

Can anyone tell me the difference between ...
  • ${HOME}
    $HOME
    ~/
I think they all point to the user's root directory (spot = /home/spot; root = /root)
What do the brackets do?

thanks,
df

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#2 Post by step »

In this case the brackets do nothing. In other cases they can serve to
1. separate the variable name from other text: ${HOME}less -> "/rootless" vs $HOMEless -> ""
2. introduce various ways to transform the variable value, i.e., ${HOME/oo/an} -> "/rant"

Note that the above discussion assumes that HOME is preset to "/root" for the root user - there can be cases when such isn't the case.

The bash shell expands ~ to the user's home directory, in this example "/root". But other shells don't do that kind of expansion, so ~ -> "~" for those shells. Bash also expands ~root to root's home directory, and more generically ~User to User's home directory for any User. Be careful relying on the ~ expansion in your scripts, because the rules for expanding ~ differ from the rules for expanding regular variables, such as HOME.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

Dry Falls
Posts: 616
Joined: Tue 16 Dec 2014, 23:37
Location: Upper Columbia

#3 Post by Dry Falls »

step wrote:In this case the brackets do nothing. In other cases they can serve to
1. separate the variable name from other text: ${HOME}less -> "/rootless" vs $HOMEless -> ""
2. introduce various ways to transform the variable value, i.e., ${HOME/oo/an} -> "/rant"

Note that the above discussion assumes that HOME is preset to "/root" for the root user - there can be cases when such isn't the case.

The bash shell expands ~ to the user's home directory, in this example "/root". But other shells don't do that kind of expansion, so ~ -> "~" for those shells. Bash also expands ~root to root's home directory, and more generically ~User to User's home directory for any User. Be careful relying on the ~ expansion in your scripts, because the rules for expanding ~ differ from the rules for expanding regular variables, such as HOME.
Thanks Step. This makes sense. I didn't want to have the last word on this thread, but I did want to thank you.

df

Post Reply