last day (14 days later) » 

3:19 PM
0
A: How to manually expand a special variable (ex: ~ tilde) in bash

GinoHere's my solution (The simpler solution at the end is probably more robust and handles ~differentusername correctly): #!/bin/bash expandTilde() { local tilde_re='^~(.*)' local path=$1 if [[ $path =~ $tilde_re ]] then path=${HOME}${BASH_REMATCH[1]} fi echo $pat...

 
Doesn't work with ~otheruser/hello (to expand to a different user's home directory).
Also, you need to quote your echo and the $1 in your example usage.
Also, relying on echo means that expandTilde -n isn't going to behave as expected, and behavior with filenames containing backslashes is undefined by POSIX. See pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html
 
Good catch. I normally use a one-user machine so I didn't think to handle that case. But I think the function could easily be enhanced to handle this other case by grepping through the /etc/passwd file for the otheruser. I'll leave it as an exercise for someone else :).
 
I've already done that exercise (and handled the OLDPWD case and others) in an answer you deemed too complex. :)
 
actually, i just found a fairly simple one-line solution that should handle the otheruser case: path=$(eval echo $orgPath)
 
Very, very insecure/dangerous. Try that with orgPath='/tmp/$(rm -rf /*)'... or, rather, don't. And also buggy because of your missing quotes.
(That's what the accepted answer is, btw, and there's a reason it has numerous folks commenting saying "don't ever do this").
...and, well, plagarizing a longstanding accepted answer isn't exactly great form.
 
3:19 PM
Dude, using "plagiarizing' in reference to a 1 line statement is a bit of an overstatement, don't you think ? In any case, I would assume that the shell script writer has full control over the invocation of the expandTilde() function with an understanding of it's limitations. Just because a statement is dangerous doesn't mean that one should avoid using it. Example: 'rm' is dangerous, since you can wipe out the filesystem with it, but that doesn't mean that you shouldn't use it. Rather, one has to be familiar with the limits of the tools one uses..
 
Sure, but if people file this in their head under "this is how I cause variables with tildes to be expanded", those caveats aren't necessarily there to be thought about every single time. Moreover, those caveats and warnings need to be propagated to the functions that use these practices, or else someone can call something that behaves dangerously without knowing it.
...so, we're not just talking about "limits" in tools, but "pitfalls". Putting pitfalls in functions you call from other functions means that you're propagating those pitfalls around your toolchain. Is that really a place you want to be?
That's why I do things the lengthy, verbose, safe way: So there's no need for a security audit to carefully analyze call graphs and make sure something didn't get reused in a place it isn't safe.
...anyhow, "probably more robust" is certainly not the case for something with definite, known security holes.
As for the argument that code that calls eval is no more dangerous than rm is, that's laughable on its face: It's like saying that a gun that occasionally explodes in the user's hand isn't more dangerous than one that fires a bullet at its target. I mean, they're both "dangerous", but the nature is entirely different: One has intended effects which could be dangerous if recklessly or intentionally misused; the other is inherently unsafe.
(I work in infosec; arguably, I'm arguing right now against my own job security, but... well, it's not like there aren't always more folks writing exploitable code to go 'round).
 

last day (14 days later) »