Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-21-2001, 02:35 AM   #1
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Rep: Reputation: 15
Question BASH string expansions

I'm a bit of a newbie to Linux and BASH and I've been BASHing my head again my monitor with a problem with string expansions. Perhaps one of you clever types can help me out....

To cut a long story short I'm 'grepping' a configuration file and extracting a number of file paths to be used for populating new project accounts. From the read string I want to expand any shell variable (such as $HOME) before using the expanded string in a 'cpy' or 'cvs' command e.g:-

When a parse and (attempt to) expand my string I always end up with the unexpanded variable still in the string. I've tried the various '${}' operators, but either I'm doing it wrong or they only seem to work on variables created within the shell.

Any help would be most appreciated.
Old 11-21-2001, 11:06 AM   #2
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The $HOME variables that the shell reads as a string is treated as any other string to the shell. What you need to do is read in the $HOME and use sed to substitute the $HOME read with the shell $HOME variable. It gets kind of tricky since sed uses the '/' character as its delimeter and the paths also contain '/'s. What I had to do is create a new home variable that has the '/'s escaped out, this I can pass to sed.


ESCHOME=`echo $HOME | sed 's/\//\\\\\//g'`

read VAR
echo $VAR | sed "s/\$HOME/${ESCHOME}/"
Old 11-21-2001, 11:40 PM   #3
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Original Poster
Rep: Reputation: 15
Thanks for that, it's an interesting idea and I'll have to add 'sed' to my many things to learn about Linux!

Unfortunately, it doesn't quite give me the result I wanted and after re-reading my initial post (and cursing at my English errors) I decided I wasn't clear enough.

Your solution has a hardcoded $HOME in the string (perhaps $HOME was a bad example), however, the string I've just extracted and stored in a variable contains (say):-


where DIRVAR has been setup globally. Basically, the DIRVAR would contain another directory path which would be used as some sort of system release i.e "beta_release/1a" or "/issue_release/2a" etc...

I've passed the read in string to a subroutine. If I then 'echo' $1 from within my bash script, that is exactly what I get (without $DIRVAR expanded). However, if I build my string manually from the command line it is displayed correctly.
It is easy to parse out the directory path and detect the '$' to denote a variable for expansion. However, I'm not sure whether I'd end up still being in the same boat. Any ideas now?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 05:49 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 07:59 AM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM
Bash problem, no executing of string ChoKamir Programming 3 01-31-2005 05:50 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 04:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

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