Visit Our UNIX and Linux User Community


variable assignment in new shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable assignment in new shell
# 1  
Old 09-02-2005
variable assignment in new shell

Hi,
I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments.
I noticed that the assignment is not working.
Please see two samples below:

Command 1:
#>ksh "i=2;echo I is $i"

Output:
#>I is


Command 2:
#>ksh <<EOF
> i=2
> echo I is $i
> EOF

Output:
#>I is

I tried, using export, typeset, set, and none of them seem to work.

Could anyone tell me what I'm doing wrong?

Thanks in advance,
Maverick80
# 2  
Old 09-02-2005
Use single quotes:
ksh 'i=2;echo I is $i'

Your internal ksh command is being constructing by an external ksh instance. ksh looks inside double quotes and replaces variables. Since i was not set, $i is replaced with nothing which is what your internal ksh will see.
# 3  
Old 09-02-2005
Thank you for your answer. I was over looking that aspect. Single quotes fixed the 1st issue.

However, is how would I get it correct, when I'm using a here document?
I.e; in the case below:
Command2:
#>ksh <<EOF
> i=2
> echo I is $i
> EOF

Output:
#>I is

Any ideas?

Thanks again,
Maverick80
# 4  
Old 09-02-2005
I understand that the parent shell would try to replace $i with the value of i (which is NULL, and hence the output).
#>ksh <<EOF
> i=2
> echo I is $i
> EOF

#> I is

So, how would be supress the parent shell, and let the new KSH to replace the value (of 2) ?


Thanks
# 5  
Old 09-02-2005
Use:
ksh <<'EOF'

Change that one line only.
# 6  
Old 09-02-2005
Awesome, it works. That was a new thing I learned. Thanks a lot.

Lastly, would you know a good site where I could find some reading material on such things? I tried searching the web. Not sure, if it was my inappropriate search string, or lack of topics, I did not find any.

Thanks again.
Regards,
Maverick80
# 7  
Old 09-02-2005
The only sites I know are in the tutorial article in our FAQ section.

Previous Thread | Next Thread
Test Your Knowledge in Computers #432
Difficulty: Medium
As of May 2017, 94.5% of the 10 million most popular web pages used JavaScript, according to w3techs.com.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Trouble with variable assignment and reading in shell scripting

Hi, I have an inputfile with some table names in it. For ex: t_arnge t_profl t_fac I need a script which reads the line one by one and need to assign to some dynamic variable. If to explain the above example: i need some a=table_name1 table_name1=t_arnge in first time and... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

4. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

Complex variable assignment

Hi, I have a requirement as follows, need to call the format of ${$var} form. For example, i am taking a variable. count=1, ((LIMIT_$count=$count + 1)) Now i have to echo this variable LIMIT_$count. (This is in a loop..) echo ${LIMIT_$count} - displays as a syntax... (3 Replies)
Discussion started by: abhisheksunkari
3 Replies

6. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

7. Shell Programming and Scripting

substituted variable assignment

I try to run this script, however, it gives an exception in line 3. How do I do an assignment to a substituted variable? #!/bin/bash name=fruit ext_$(eval echo ${name})=apple tmp=ext_$(eval echo ${name}) if ]; then echo "apple" elif ]; then echo "orange" fi echo ${!tmp} Error... (2 Replies)
Discussion started by: angelokh
2 Replies

8. Shell Programming and Scripting

Dynamic variable assignment

Hi All, I have the below scenario: A file test.cfg with three fields>> DATA1 DATA2 DATA3 In the script I need to assign each of the fields to variables. The number of fields will not be constant (this case we have three). Im trying to do something like this: NUM=1 OUT_DAT_NO=3 ... (4 Replies)
Discussion started by: deepakgang
4 Replies

9. Shell Programming and Scripting

Variable assignment question

Hello, I would like to assign number of lines in a file to a variable (to be passed later as an argument to a function). I am doing it like this: numLines=wc -l < file.txt which gives an error. Could somebody help? (2 Replies)
Discussion started by: DDD
2 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies

Featured Tech Videos