Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Dianne Skoll
On Thu, 17 Jun 2021 11:35:19 -0400
jean-Francois Messier  wrote:

> I would also assume that if this command was run as root it would not
> work, as root's home directory is /root.

DIR="$(echo ~)/Something/" would "fix" that, but...

... this is why you should not use shell programming for anything
serious or production-level, IMO.  Its quoting rules are traps for the
unwary.  Better to use a proper language like Python, Perl or
whatever.

Regards,

Dianne.

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Alex Pilon
On Thu 2021-06-17 14:26:01 -0400, Alex Pilon wrote:
> On Thu 2021-06-17 13:43:43 -0400, Ian! D. Allen wrote:
> > As others have said, quoting (even double quoting) hides tilde expansion.
> > Just make sure the tilde isn't inside quotes and it works fine:
> >
> > DIR=~/"Something"
>
> Strictly speaking per spec this won't work. Tilde expansion happens
> before parameter substitution. If it happens in any other order, that is
> undefined behaviour and a portability issue.

I misread that. That's my fault. I was thinking of some other thread
when referring to other users.

Ian's right.

Apologies for the spam list.

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Alex Pilon
On Thu 2021-06-17 13:43:43 -0400, Ian! D. Allen wrote:
> As others have said, quoting (even double quoting) hides tilde expansion.
> Just make sure the tilde isn't inside quotes and it works fine:
>
> DIR=~/"Something"

Strictly speaking per spec this won't work. Tilde expansion happens
before parameter substitution. If it happens in any other order, that is
undefined behaviour and a portability issue.

So § 2.6 ¶ 2 of the 2018 rev of the spec.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06

> > Heck, I'd just if [ -d ~/Something ]; then
>
> No, not if "Something" is text or a variable that could include spaces
> or GLOB characters.  Always double-quote your variables.

This is a literal. We all know what we are doing for space-less
literals. I would never imply otherwise for variables. It's in my
company policy, that I wrote, that there will be no exceptions even if
you know that the inputs are always safe, and it's there because humans
consistently make mistakes at least some times (or all the time for
juniors developers who learned shell script chaotically or haphazardly).

> None of the above is needed if you only want the HOME directory of the
> person running the script.  All that work is already done for you and
> sitting in your $HOME environment variable.  The one line:
>
> DIR="$HOME/Something"
>
> Works for any user, even root, even on old shells that don't expand
> tildes, even if $HOME or Something contains blanks or GLOB characters.

I'm pretty sure that JFM meant that it doesn't look up the home of the
original user if you ran it under sudo (and expect nothing from su or
other). That'd be $SUDO_USER then, though it's only 40 more characters
to command sub then cut -d : -f …, like I said in a private email.

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Ian! D. Allen
> DIR="/home/$(basename ~)/Something/"

As others have pointed out, it's the unquoted tilde in the command
substitution that is expanding above.  You could have used this (but
don't):

DIR="$(echo ~)/Something/"

As others have said, quoting (even double quoting) hides tilde expansion.
Just make sure the tilde isn't inside quotes and it works fine:

DIR=~/"Something"

> Heck, I'd just if [ -d ~/Something ]; then

No, not if "Something" is text or a variable that could include spaces
or GLOB characters.  Always double-quote your variables.

Since the tilde is exactly equivalent to $HOME, this is cleanest:

DIR="$HOME/Something"
if [ -d "$DIR" ] ; then ...

> I would also assume that if this command was run as root it would not
> work, as root's home directory is /root. Same for any other user whose
> home directory is not under /home. Perhaps a few-lines script would
> first have to grep the login ID from /etc/passwd (read-only, so OK),
> parse the line to get the actual home directory and return it.

None of the above is needed if you only want the HOME directory of the
person running the script.  All that work is already done for you and
sitting in your $HOME environment variable.  The one line:

DIR="$HOME/Something"

Works for any user, even root, even on old shells that don't expand
tildes, even if $HOME or Something contains blanks or GLOB characters.

*ALWAYS DOUBLE QUOTE YOUR VARIABLE EXPANSIONS*

https://teaching.idallen.org/cst8207/19w/notes/320_shell_variables.html#double-quote-all-uses-of-variables

-- 
| Ian! D. Allen, BA, MMath  -  idal...@idallen.ca - Ottawa, Ontario, Canada
| Home: www.idallen.com   Contact Improvisation Dance: www.contactimprov.ca
| Former college professor (Free/Libre GNU+Linux) at:  teaching.idallen.com
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



Re: [linux] expansion of tilde for home directory

2021-06-17 Thread jean-Francois Messier
I would also assume that if this command was run as root it would not
work, as root's home directory is /root. Same for any other user whose
home directory is not under /home. Perhaps a few-lines script would
first have to grep the login ID from /etc/passwd (read-only, so OK),
parse the line to get the actual home directory and return it.

On 2021-06-17 10:55 a.m., J C Nash wrote:
> While I've found a workaround, I was a bit surprised that a test for 
> existence of
> a directory in a bash script did not work.
>
> I tried
>
> DIR="~/Something/"
> if [ -d "$DIR" ]; then
>
> and got that the directory did NOT exist when it was clearly there.
>
> Workaround was to use
>
> DIR="/home/$(basename ~)/Something/"
>
> I did a bit of a search, but found no mention of the fact that ~ is not
> expanded in the if [  ] construct. Is it, in fact, "well-known" except
> to me?
>
> Cheers, JN
>
>
>
> To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
> To get help send a blank message to linux+h...@linux-ottawa.org
> To visit the archives: https://lists.linux-ottawa.org
>

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Shawn H Corey

On 2021-06-17 10:55 a.m., J C Nash wrote:

DIR="~/Something/"
if [ -d "$DIR" ]; then



    DIR=~/Something/
    if [ -d "$DIR" ]; then

or

    DIR=~/"Something with spaces/"
    if [ -d "$DIR" ]; then




Re: [linux] expansion of tilde for home directory

2021-06-17 Thread Alex Pilon
On Thu 2021-06-17 10:55:27 -0400, J C Nash wrote:
> While I've found a workaround, I was a bit surprised that a test for 
> existence of
> a directory in a bash script did not work.

This is POSIX shell in general.

> I tried
>
> DIR="~/Something/"

Just DIR="$HOME/Something" unless you need to refer to someone else's
home.

> if [ -d "$DIR" ]; then

Heck, I'd just if [ -d ~/Something ]; then

> and got that the directory did NOT exist when it was clearly there.
>
> Workaround was to use
>
> DIR="/home/$(basename ~)/Something/"
>
> I did a bit of a search, but found no mention of the fact that ~ is not
> expanded in the if [  ] construct. Is it, in fact, "well-known" except
> to me?

It's because the tilde is in quotes.

See 2018 POSIX shell command language § 2.6.1 Tilde Expansion

A "tilde-prefix" consists of an unquoted  character at the
beginning of a word, followed by all of the characters preceding the
first unquoted  in the word, or all the characters in the
word if there is no .

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_01

Your tilde in your command substitution is unquoted, which is why it
works.

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org



[linux] expansion of tilde for home directory

2021-06-17 Thread J C Nash
While I've found a workaround, I was a bit surprised that a test for existence 
of
a directory in a bash script did not work.

I tried

DIR="~/Something/"
if [ -d "$DIR" ]; then

and got that the directory did NOT exist when it was clearly there.

Workaround was to use

DIR="/home/$(basename ~)/Something/"

I did a bit of a search, but found no mention of the fact that ~ is not
expanded in the if [  ] construct. Is it, in fact, "well-known" except
to me?

Cheers, JN



To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org