LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-24-2007, 09:43 PM   #1
champak
LQ Newbie
 
Registered: Nov 2007
Posts: 20

Rep: Reputation: 0
Variable expansion in BASH


Hi..

Im reading values from a file line by line.
Sample File
..
first|blah
second|$some_shell_var
...

I have to process these values like
...
source=$(echo $line | cut -d'|' -f1)
dest=$(echo $line | cut -d'|' -f2)

echo $source
echo $dest
...

When I echo the values I get 'blah' for the first line(first|blah).
For the second line I get '$some_shell_var'. But instead I want the variable value to be expanded(I mean I want the $some_shell_var to be substituted with its value). How this can be done in BASH?

Any ideas?

Thanks
 
Old 11-24-2007, 11:11 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Code:
#check if dest starts with a '$'
if [ ${dest:0:1} = '$' ] ; then
   #remove the '$'
   dest="${dest:1}"
   #{!dest} uses indirection, 
   #search for variable indirection in the bash man page for details
   echo "source = $source, dest = ${!dest}"
else
   echo "source = $source, dest = $dest"
fi
 
Old 11-24-2007, 11:36 PM   #3
champak
LQ Newbie
 
Registered: Nov 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks for a lot ntubski.
But just checking if there's any direct approach to get the variable expanded without removing the '$' char
 
Old 11-25-2007, 12:36 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
I guess you could use eval:

Code:
eval "echo dest = ${dest}"
But if you have something in your file like
Code:
second|$(rm -rf /*)
bad things could happen...
 
Old 11-26-2007, 01:19 AM   #5
champak
LQ Newbie
 
Registered: Nov 2007
Posts: 20

Original Poster
Rep: Reputation: 0
ya thx again
 
Old 11-26-2007, 03:44 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,305

Rep: Reputation: 175Reputation: 175
try this,
Code:

IFS=\|
while read f1 f2; do
    eval source="$f1"
    eval dest="$f2"
done

Last edited by bigearsbilly; 11-26-2007 at 03:49 AM.
 
  


Reply


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
!command expansion in csh vs. bash jhwilliams Linux - Software 9 08-27-2007 06:04 PM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 12:45 PM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 10:09 AM
bash script $n variable expansion cortez Programming 6 12-08-2003 05:03 PM


All times are GMT -5. The time now is 06:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration