LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-08-2012, 01:51 PM   #1
mohitvad
Member
 
Registered: Feb 2010
Posts: 34

Rep: Reputation: 0
set variable in bash


Hi,

I've problem that i want to print variable with giving a argument (colo) to script and print it . but i am getting an error. Please help me to fix the syntax .Thanks in advance

#!/bin/bash

colo_file="jinda"

echo ${$1_file}
 
Old 09-08-2012, 02:00 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello,

I think you meant this:
Code:
#!/bin/bash

colo_file="jinda"

echo $1_file
Markus
 
Old 09-08-2012, 02:11 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi Markus!

Maybe I'm wrong, but I think the question is how to retrieve the value of the variable, that is "jinda". I think it's a task of indirect variable reference:
Code:
#!/bin/bash
colo_file="jinda"
varname=$1_file
echo ${!varname}
The variable name is built using the argument, then it's indirectly referenced in the echo statement (uisng the notation introduced in bash version 2).
 
1 members found this post helpful.
Old 09-08-2012, 02:22 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Hi Markus!

Maybe I'm wrong, but I think the question is how to retrieve the value of the variable, that is "jinda". I think it's a task of
...
Well colucix, probably you're right, I misread the question.

Markus
 
Old 09-08-2012, 03:34 PM   #5
mohitvad
Member
 
Registered: Feb 2010
Posts: 34

Original Poster
Rep: Reputation: 0
Hey colucix,

Thanks buddy, it solved the problem. Can i get bit more information about notation/indirect reference to the variable ${!varname}.
 
  


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: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
Is it possible to set an environment variable name containing a period in BASH? dnwake Linux - General 7 03-28-2010 01:05 PM
More bash weirdness, variable is not being set MrUmunhum Linux - General 3 02-08-2010 04:01 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
how do I set a system wide enviroment variable? (not just bash) drkstr Linux - Newbie 11 04-19-2006 09:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:01 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