[opensuse] 11.4 BASH - tilde expansion - isn't expanding when used in variable? Help?

Guys, Here is a strange bashism. I want to check that ~/tde/tgz exists or I want to create the directory. So I do: # set tgzdir for output of tarballs tgzdir="${2:-~/tde/tgz}" [[ -d "$tgzdir" ]] || mkdir -p "$tgzdir" To my surprise, it created a directory named '~' in the pwd: scr/~/ scr/~/tde/ scr/~/tde/tgz/ Huh?? I know I could use $HOME instead, but I want to understand what rule I'm violating. Seems like I've successfully used this in the past. What say the gurus? -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org

On Saturday 14 May 2011 21:54:41 David C. Rankin wrote:
# set tgzdir for output of tarballs tgzdir="${2:-~/tde/tgz}"
Looks like the problem is that it is quoted
echo ${2:-~} /home/anders echo "${2:-~}" ~
Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org

On Saturday 14 May 2011 21:54:41 David C. Rankin wrote:
[[ -d "$tgzdir" ]] || mkdir -p "$tgzdir"
Incidentally, if you use -p you don't have to test to see if it exists first. mkdir -p will simply exit silently if the directory already exists Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org

On Saturday May 14 2011, Anders Johansson wrote:
On Saturday 14 May 2011 21:54:41 David C. Rankin wrote:
[[ -d "$tgzdir" ]] || mkdir -p "$tgzdir"
Incidentally, if you use -p you don't have to test to see if it exists first. mkdir -p will simply exit silently if the directory already exists
It also allows you to make a deep path without going step by step down in. So... % mkdir -p /tmp/foo/bar/baz/oh-boy ... does what this does without -p: % mkdir /tmp/foo % mkdir /tmp/foo/bar % mkdir /tmp/foo/bar/baz % mkdir /tmp/foo/bar/baz/oh-boy
Anders
By the way, I don't think it's a bug that ~ is not expanded within quotes. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org

On Saturday May 14 2011, Randall R Schulz wrote:
On Saturday May 14 2011, Anders Johansson wrote:
...
Incidentally, if you use -p you don't have to test to see if it exists first. mkdir -p will simply exit silently if the directory already exists
It also allows you to make a deep path without going step by step down in. So...
% mkdir -p /tmp/foo/bar/baz/oh-boy
... does what this does without -p:
% mkdir /tmp/foo % mkdir /tmp/foo/bar % mkdir /tmp/foo/bar/baz % mkdir /tmp/foo/bar/baz/oh-boy
Or this, of course: % mkdir /tmp/foo /tmp/foo/bar /tmp/foo/bar/baz /tmp/foo/bar/baz/oh-boy Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org

Hello, On Sat, 14 May 2011, Randall R Schulz wrote:
On Saturday May 14 2011, Anders Johansson wrote:
On Saturday 14 May 2011 21:54:41 David C. Rankin wrote:
[[ -d "$tgzdir" ]] || mkdir -p "$tgzdir" [..] By the way, I don't think it's a bug that ~ is not expanded within quotes.
It is not. ==== man bash ==== Tilde Expansion If a word begins with an unquoted tilde character (`~'), [..] ^^^^^^^^ ==== BTW: to get rid of that '~' dir, use perl[1] or maybe './~/' (don't know if the latter works). Oh, and you should probably use home=$(getent passwd <USERNAME> | cut -d: -f6) anyway instead of using '~'. If that's overkill, let the tilde expand by not quoting at the right stage: $ foo=~/"bar" $ echo "$foo" /home/dh/bar I.e.: do NOT quote the initial ~/, but put the rest in "". Sidenote: You need not to quote the / after ~. Wierd bashism I'd guess. So: tgzdir=~/"whatever" ... should work for you. HTH, -dnh [1] perl -e 'unlink("/path/to/weird/~");' or something like that. -- All Hardware Sucks and I do not consider myself to actually have any data until there's an offsite backup of it. -- Anthony de Boer -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Anders Johansson
-
Dave Howorth
-
David C. Rankin
-
David Haller
-
Randall R Schulz