HOME vs ~

  • Follow


What is the proper way to refer to the home directory
in a Bourne shell?

e.g. "cd $HOME/bin" works, but
"cd ~/bin" doesn't always seem to work.

/Why Tea
0
Reply ytlim1 (236) 2/7/2011 9:32:32 AM

Why Tea wrote:
> What is the proper way to refer to the home directory
> in a Bourne shell?
>
> e.g. "cd $HOME/bin" works always , but
                             ^^^^^^

> "cd ~/bin" doesn't always seem to work.

Obvious, isn't it?
0
Reply PM 2/7/2011 1:23:43 PM


Why Tea <ytlim1@gmail.com> writes:

> What is the proper way to refer to the home directory in a Bourne
> shell?

The ‘HOME’ variable.

> e.g. "cd $HOME/bin" works

What would that command line look like if the ‘HOME’ variable were not
set? Would you then expect the resulting command to work?

Try another example: would you expect ‘ls $HOME/.profile’ to always
work?

> "cd ~/bin" doesn't always seem to work.

The ‘~’ expansion is a shortcut for convenience. Under what
circumstances does it not work for you?

-- 
 \         “I planted some bird seed. A bird came up. Now I don't know |
  `\                                  what to feed it.” —Steven Wright |
_o__)                                                                  |
Ben Finney
0
Reply Ben 2/7/2011 9:32:34 PM

Ben Finney <ben+unix@benfinney.id.au> writes:
> Why Tea <ytlim1@gmail.com> writes:
>
>> What is the proper way to refer to the home directory in a Bourne
>> shell?
>
> The ‘HOME’ variable.

Does that imply that ~ is somehow improper?  As far as I can tell from
the documentation, they should behave identically (except for how it
deals with characters immediately following it).

[...]

>> "cd ~/bin" doesn't always seem to work.
>
> The ‘~’ expansion is a shortcut for convenience. Under what
> circumstances does it not work for you?

That's the question.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply Keith 2/8/2011 1:30:50 AM

Keith Thompson <kst-u@mib.org> writes:

> Ben Finney <ben+unix@benfinney.id.au> writes:
> > Why Tea <ytlim1@gmail.com> writes:
> >
> >> What is the proper way to refer to the home directory in a Bourne
> >> shell?
> >
> > The ‘HOME’ variable.
>
> Does that imply that ~ is somehow improper?

That wasn't my intention.

-- 
 \     “We used to laugh at Grandpa when he'd head off and go fishing. |
  `\      But we wouldn't be laughing that evening when he'd come back |
_o__)              with some whore he picked up in town.” —Jack Handey |
Ben Finney
0
Reply Ben 2/8/2011 1:40:40 AM

On February 7, 2011 04:32, in comp.unix.shell, ytlim1@gmail.com wrote:

> What is the proper way to refer to the home directory
> in a Bourne shell?

Like a lot of questions, the answer to this one is "it depends".
 
> e.g. "cd $HOME/bin" works, but
> "cd ~/bin" doesn't always seem to work.

As others have asked, what circumstances are you concerned about here?
As I read it, ~ won't expand to $HOME
  a) if tilde-expansion has been disabled, or
  b) if $HOME is unset

Now, $HOME is originally set from the "user home directory" field of the
user's /etc/passwd file entry. But, $HOME can be altered anywhere during
the login or shell startup process by options in the login.defs file or
through Bash set/export commands in /etc/profile, /etc/profile.d/*,
~/.profile, or any of the other "included" shell scripts.

As for the "proper way" to refer to the home directory, I guess it depends
on what features you have enabled and what environment variables you trust.
At worst case, the user's home directory is that which is recorded in
the /etc/passwd entry, but $HOME and ~ /may/ be used, if you trust them.

> /Why Tea

HTH
-- 
Lew Pitcher
Master Codewright & JOAT-in-training   | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
----------      Slackware - Because I know what I'm doing.         ------


0
Reply Lew 2/8/2011 1:59:03 AM

Lew Pitcher <lpitcher@teksavvy.com> writes:
> On February 7, 2011 04:32, in comp.unix.shell, ytlim1@gmail.com wrote:
>
>> What is the proper way to refer to the home directory
>> in a Bourne shell?
>
> Like a lot of questions, the answer to this one is "it depends".
>  
>> e.g. "cd $HOME/bin" works, but
>> "cd ~/bin" doesn't always seem to work.
>
> As others have asked, what circumstances are you concerned about here?
> As I read it, ~ won't expand to $HOME
>   a) if tilde-expansion has been disabled, or
>   b) if $HOME is unset

For some reason, I assumed the original question was about bash,
but I see it was about "a Bourne shell".

In bash, as far as I can tell from the documentation, there's no way
to disable ~ expansion.  Likewise for ksh.  (The zsh documentation
has some stuff about ~ expansion, but I haven't taken the time to
figure it out.)

The original Bourne shell didn't do ~ expansion at all (I think it
was originally a csh innovation), and in fact /bin/sh on Solaris
9 doesn't do it.  So $HOME is probably more portable.

But the OP's statement that

    "cd ~/bin" doesn't always seem to work.

is confusing.  Unless he comes back and explains the circumstances,
we can't guess what the problem might be.

> Now, $HOME is originally set from the "user home directory" field of the
> user's /etc/passwd file entry. But, $HOME can be altered anywhere during
> the login or shell startup process by options in the login.defs file or
> through Bash set/export commands in /etc/profile, /etc/profile.d/*,
> ~/.profile, or any of the other "included" shell scripts.
>
> As for the "proper way" to refer to the home directory, I guess it depends
> on what features you have enabled and what environment variables you trust.
> At worst case, the user's home directory is that which is recorded in
> the /etc/passwd entry, but $HOME and ~ /may/ be used, if you trust them.

In the worst case, the /etc/passwd file doesn't contain an entry for the
current user, and it has to be obtained by some other method (YP, NIS,
etc.).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply Keith 2/9/2011 1:26:06 AM

2011-02-7, 01:32(-08), Why Tea:
> What is the proper way to refer to the home directory
> in a Bourne shell?
>
> e.g. "cd $HOME/bin" works, but
> "cd ~/bin" doesn't always seem to work.
[...]

From http://www.in-ulm.de/~mascheck/bourne/ only one (esoteric)
variant of the Bourne shell supported tilde expansion and
probably not by default.

The Bourne shell, like the Thomson shell before it has been
obsoleted a long time ago though. Now "sh" (though some systems
have several ones, one of which may still be a Bourne shell or
behave as such under some circumstance) refers to an
implementation or another of the POSIX shell specification (or
one version or the other of that specification).

And tilde expansion is a required feature of the POSIX shell.

However (even if most shells don't implement that misfeature),
tilde expansion is meant to be performed before word splitting
and filename generation, which makes it unusable except in
situations where those don't occur (for instance in variable
assignment or "case"), or if one can be sure that the expansion
wouldn't trigger those further expansions (for ~, that would
mean if you can guaranty that $HOME doesn't contain any *, [ or
? (and possibly more for some shells) nor any IFS character).

Also, in a POSIX shell, cd has two modes the default one being
not the one you'd expect. In the default mode, cd -- "$HOME"
doesn't necessarily take you to your home directory (though it
would in the majority of cases).

So, in a POSIX shell script, the correct way would be:

cd -P -- "$HOME" || exit

or even better:

cd -P || exit

instead.


If you want to extend the portability of your script to very old
systems or to any shell in the Bourne family, you'd write

cd || exit

knowing that it may not work or may not take you to the correct
directory in some unusual circumstances.

for instance, with

HOME=/tmp/..

where /tmp is a symlink to /var/tmp, your home directory is /var,
"cd" in the Bourne shell would take you there but in a POSIX
shell it would take you to / instead while "cd -P" would take
you to "/var".

-- 
Stephane
0
Reply Stephane 2/9/2011 10:41:07 AM

In article <slrnil4rq3.74d.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
>
>However (even if most shells don't implement that misfeature),
>tilde expansion is meant to be performed before word splitting
>and filename generation, which makes it unusable except in
>situations where those don't occur (for instance in variable
>assignment or "case"), or if one can be sure that the expansion
>wouldn't trigger those further expansions (for ~, that would
>mean if you can guaranty that $HOME doesn't contain any *, [ or
>? (and possibly more for some shells) nor any IFS character).

If your home directory has shell metacharacters in its name, shoot your
sysadmin.

-- 
Alan Curry
0
Reply pacman 2/9/2011 10:25:27 PM

pacman@kosh.dhis.org (Alan Curry) writes:

> In article <slrnil4rq3.74d.stephane.chazelas@spam.is.invalid>,
> Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
> >that would mean if you can guaranty that $HOME doesn't contain any *,
> >[ or ? (and possibly more for some shells) nor any IFS character).
>
> If your home directory has shell metacharacters in its name, shoot
> your sysadmin.

Torture them first to get the root credentials. Then shoot them.

-- 
 \       “The surest way to corrupt a youth is to instruct him to hold |
  `\       in higher esteem those who think alike than those who think |
_o__)             differently.” —Friedrich Nietzsche, _The Dawn_, 1881 |
Ben Finney
0
Reply Ben 2/10/2011 12:31:17 AM

On 2/9/2011 7:31 PM, Ben Finney wrote:
> pacman@kosh.dhis.org (Alan Curry) writes:
> 
>> In article <slrnil4rq3.74d.stephane.chazelas@spam.is.invalid>,
>> Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
>>> that would mean if you can guaranty that $HOME doesn't contain any *,
>>> [ or ? (and possibly more for some shells) nor any IFS character).
>>
>> If your home directory has shell metacharacters in its name, shoot
>> your sysadmin.
> 
> Torture them first to get the root credentials. Then shoot them.
> 

You're right of course, but that won't stop some home users from
using spaces in directory names.  So as usual Stephane's advice is
probably best and in scripts meant to be portable, avoid using ~.

-- 
Wayne
0
Reply Wayne 2/10/2011 6:23:31 PM

2011-02-9, 22:25(+00), Alan Curry:
> In article <slrnil4rq3.74d.stephane.chazelas@spam.is.invalid>,
> Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
>>
>>However (even if most shells don't implement that misfeature),
>>tilde expansion is meant to be performed before word splitting
>>and filename generation, which makes it unusable except in
>>situations where those don't occur (for instance in variable
>>assignment or "case"), or if one can be sure that the expansion
>>wouldn't trigger those further expansions (for ~, that would
>>mean if you can guaranty that $HOME doesn't contain any *, [ or
>>? (and possibly more for some shells) nor any IFS character).
>
> If your home directory has shell metacharacters in its name, shoot your
> sysadmin.

It's not uncommon for HOME to have space like in
/cygdrive/c/Documents and Settings/joe

And IFS can also be modified.

-- 
Stephane
0
Reply Stephane 2/10/2011 8:24:46 PM

Wayne <nospam@all.invalid> writes:

> You're right of course, but that won't stop some home users from
> using spaces in directory names.  So as usual Stephane's advice is
> probably best and in scripts meant to be portable, avoid using ~.

Users cannot, at least, put shell metacharacters into the name of their
home directory (because they don't get to choose or rename that
directory).

So with the offending sysadmin shot and the root credentials under one's
control, that problem can be fixed, making ‘~’ expansion safe again for
all. Hooray!

-- 
 \       “A ‘No’ uttered from deepest conviction is better and greater |
  `\       than a ‘Yes’ merely uttered to please, or what is worse, to |
_o__)                              avoid trouble.” —Mohandas K. Gandhi |
Ben Finney
0
Reply Ben 2/10/2011 8:38:12 PM

On 2011-02-08 Tue 20:26:06, Keith Thompson wrote:
> Lew Pitcher <lpitcher@teksavvy.com> writes:
>> On February 7, 2011 04:32, in comp.unix.shell, ytlim1@gmail.com wrote:
>>
>>> What is the proper way to refer to the home directory
>>> in a Bourne shell?
>>
>> Like a lot of questions, the answer to this one is "it depends".
>>  
>>> e.g. "cd $HOME/bin" works, but
>>> "cd ~/bin" doesn't always seem to work.
>>
>> As others have asked, what circumstances are you concerned about here?
>> As I read it, ~ won't expand to $HOME
>>   a) if tilde-expansion has been disabled, or
>>   b) if $HOME is unset
>
> For some reason, I assumed the original question was about bash,
> but I see it was about "a Bourne shell".
>
> In bash, as far as I can tell from the documentation, there's no way
> to disable ~ expansion.

If you quote the tilde, it will not be expanded.

    cd "~/bin"

will fail unless a directory named '~' is in the current directory and
contains a bin. So if the OP is doing something like

MYDIR="~/bin"
cd $MYDIR

it'll fail.
0
Reply slakmagik 2/10/2011 9:45:38 PM

In article <slrnil8ice.ge2.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
>2011-02-9, 22:25(+00), Alan Curry:
>>
>> If your home directory has shell metacharacters in its name, shoot your
>> sysadmin.
>
>It's not uncommon for HOME to have space like in
>/cygdrive/c/Documents and Settings/joe

Does cygwin count as "unix" these days? Ugh.

-- 
Alan Curry
0
Reply pacman 2/10/2011 10:28:40 PM

Stephane CHAZELAS <stephane_chazelas@yahoo.fr> wrote:
> 2011-02-7, 01:32(-08), Why Tea:
>> What is the proper way to refer to the home directory
>> in a Bourne shell?
>>
>> e.g. "cd $HOME/bin" works, but
>> "cd ~/bin" doesn't always seem to work.
> [...]
> 
> From http://www.in-ulm.de/~mascheck/bourne/ only one (esoteric)

Hillarious. Only Germans would come up with such a detailed description of 
legacy code and behavior. I've never heard of Sinix until reading that 
either.
0
Reply Cydrome 2/10/2011 10:42:41 PM

pacman@kosh.dhis.org (Alan Curry) writes:
> In article <slrnil8ice.ge2.stephane.chazelas@spam.is.invalid>,
> Stephane CHAZELAS  <stephane_chazelas@yahoo.fr> wrote:
>>2011-02-9, 22:25(+00), Alan Curry:
>>>
>>> If your home directory has shell metacharacters in its name, shoot your
>>> sysadmin.
>>
>>It's not uncommon for HOME to have space like in
>>/cygdrive/c/Documents and Settings/joe
>
> Does cygwin count as "unix" these days? Ugh.

Probably not, but it certainly counts as a system that supports a whole
bunch of Unix-like libraries and commands, including several shells.

It's also possible for a user to change the value of $HOME (which also
affects the expansion of ~).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply Keith 2/11/2011 12:33:45 AM

16 Replies
176 Views

(page loaded in 0.174 seconds)

Similiar Articles:


















7/8/2012 5:12:17 PM


Reply: