bash scripting: tilde expansion with the read builtin
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash scripting: tilde expansion with the read builtin
Hello world,
This is my first post on LQ. I have been banging my head on that one for an hour or two now ...
With bash, I noticed that you cannot do tilde expansion on input from the read builtin. That is very annoying. Compare:
[HTML]
#!/bin/bash
test=~/Desktop
if [ -d "$test" ]; then
echo "$test" # tilde expansion work, "/home/etienne/Desktop" is echoed
fi
read test2
if [ -d "$test2" ]; then
echo "$test2" # tilde expansion will not be done on test2
fi
[/HTML]
Tilde expansion in this context would be very helpful. For example, when writing an interactive script that is supposed to read directory names and act upon them.
Any way I can do tilde expansion on the result of the read builtin ? Or should I try another approach entirely ?
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
That doesn't solve the problem either. The issue is that `read test` takes input from standard input literally without doing a shell scan and substitution of the input string. Once it is in a variable, you can't get recursive substitution. So, you never get the substitution in that original literal string that came in with `read test`.
However, you can request a shell processing of a string using the eval shell builtin command.
$ read test
$ eval test1=$test
will take the "~/Desktop" that is in test and end up with "/home/etienne/Desktop" in test1.
Last edited by choogendyk; 09-23-2007 at 08:44 PM.
great! i've been searching for this for 2 days, and now I found it.
thanks!
although, I found a potential problem with this: it is possible to inject bash commands
like this:
$ read test
~/directory;exit
$ eval something=$test
you can strip extra commands by using the bash specific syntax (probably easy to do it the /bin/sh compatible way, but I'm too lazy):
$ eval something=${test%%;*}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.