LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2007, 05:50 PM   #1
EtienneG
Canonical
 
Registered: Sep 2007
Posts: 3

Rep: Reputation: 0
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 ?

Many thanks in advance!
 
Old 09-23-2007, 06:47 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Try using the bash shell variable $HOME instead. e.g. $HOME/Desktop/...
 
Old 09-23-2007, 08:42 PM   #3
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
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.
 
Old 09-23-2007, 09:31 PM   #4
EtienneG
Canonical
 
Registered: Sep 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by choogendyk View Post
However, you can request a shell processing of a string using the eval shell builtin command.
Right on, dude!

Thanks a lot. That's very clever, if somewhat unobvious. It would never have came to me.
 
Old 09-26-2007, 04:10 PM   #5
LeLutin
LQ Newbie
 
Registered: Sep 2007
Distribution: Debian, Ubuntu
Posts: 5

Rep: Reputation: 0
Thumbs up

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%%;*}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM
Bash Script Expansion Problem meadensi Linux - Newbie 1 03-04-2005 05:17 PM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
Scripting help -- pathname expansion player_2 Programming 3 08-25-2003 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration