LinuxQuestions.org
Register a domain and help support LQ
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, 02: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, 03:00 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello,

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

colo_file="jinda"

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

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 03:22 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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, 04: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


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


All times are GMT -5. The time now is 09:52 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration