LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2018, 11:24 AM   #1
shravee
Member
 
Registered: Sep 2009
Posts: 84

Rep: Reputation: 16
Post unable to concatenate the variable dynamically


i have few variables defined in my env file.
for eg, CH=/this/is/first CH2=/this/is/second


i am scanning for few files using a script in a while loop with i as a counter.

echo $CH gives the value of the path. /this/is/first

while [ i equals to 2 ]
do

echo $CH$i is giving me the value of the path /this/is/first2 ( expected is /this/is/second)

done


how do i get this taken care dynamically.

i want CH to be appended with the "i" dynamically so that it becomes to CH2 and then finally gives the value of $CH2

i have many more such variables to be taken care. kind of stuck here.
 
Old 02-13-2018, 12:24 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
What you need is variable indirection.
Code:
Varname=CH$i
echo ${!Varname}

Last edited by rknichols; 02-13-2018 at 12:26 PM.
 
Old 02-13-2018, 01:11 PM   #3
shravee
Member
 
Registered: Sep 2009
Posts: 84

Original Poster
Rep: Reputation: 16
did not work. am i missing something.

1st attempt:

/ $ echo varname=CH$i
varname=CH2
/ $ echo ${!varname}
varname


2nd attempt:

/ $ pwd
/
/ $ echo $i
2
/ $ echo $CH2
/cognos/webcontent/ibm/c10_2_1_p17300
/ $ var=CH$i; echo ${!var}
var
/ $
 
Old 02-13-2018, 02:56 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
What shell are you using?
Code:
$ CH2=/path/to/some/file
$ i=2
$ var=CH$i; echo ${!var}
/path/to/some/file
$ echo $BASH_VERSION
4.1.2(2)-release
 
1 members found this post helpful.
Old 02-13-2018, 03:26 PM   #5
shravee
Member
 
Registered: Sep 2009
Posts: 84

Original Poster
Rep: Reputation: 16
/cognos $ echo $SHELL
/bin/ksh
 
Old 02-13-2018, 03:38 PM   #6
shravee
Member
 
Registered: Sep 2009
Posts: 84

Original Poster
Rep: Reputation: 16
it worked when i entered bash and turned into bash-3.2$ prompt.

do i need to this all the time. moreover, does it work automatically in a script.
 
Old 02-13-2018, 03:44 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Just insert a line "#!/bin/bash" as the very first line in the file and it should work just fine. Absent that line, your current shell will try to fork a copy of itself to run the script, and obviously ksh does not support that form of variable indirection.
 
1 members found this post helpful.
  


Reply

Tags
variable



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate string through variable in awk beca123456 Linux - Newbie 1 11-25-2016 03:13 AM
Environment variable that dynamically changes when another variable is changed youtoo Linux - Newbie 4 02-17-2016 01:44 AM
[SOLVED] Concatenate variable w/ path string ... spaghettios Linux - Newbie 4 07-22-2012 05:50 PM
concatenate variable names in bash xeon123 Linux - Newbie 4 04-07-2011 08:01 PM
how to concatenate variable name in Java ? black Programming 1 11-16-2004 04:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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