Variable Assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable Assignment
# 1  
Old 07-17-2016
Variable Assignment

Hi

I am facing a problem.

Code:
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:
Code:
export ABC_HOME=/opt/app/abc/$abcversion/

echo "$ABC_HOME"

It prints
Code:
/opt/app/abc/abc*/

How can i get the actual value of abcversion in another variable assignment?

I tried searching the net and various tweaks but didnt work.

Thanks
# 2  
Old 07-17-2016
When you assign
Code:
abcversion="abc*"

then $abcversion will contain just "abc*". It will not be set to "abcga5" because the quotes prevent globbing.
In the next line you do:
Code:
 echo $abcversion

which is expanded by the shell to
echo abc* and only then the abc* is expanded to abcga5.

If you do want the variable "abcversion" have the value of abcga5 you should omit the quotes in the assignment:
Code:
abcversion=abc*


Last edited by Ivo Breeden; 07-17-2016 at 08:06 PM.. Reason: typos
# 3  
Old 07-17-2016
I tried removing the quotes and here are the results:

Code:
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 )

Code:
export ABC_HOME=/opt/app/abc/$abcversion/

echo "$ABC_HOME"

/opt/app/abc/abc*/

but I need this:

Code:
/opt/app/abc/abcga5

# 4  
Old 07-18-2016
The double quotes prevent wildcard expansion, but not in the assignment (var=pattern* is the same as var="pattern*"),

but they do make a difference when used with the echo statement. To get the wildcard * to match, they should be left out:
Code:
echo $ABC_HOME

However, if there are multiple matches then you would get this:
Code:
echo $ABC_HOME
/opt/app/abc/abcga5 /opt/app/abc/abcga6 /opt/app/abc/abcga7

# 5  
Old 07-18-2016
Hi

Its still not working. i removed quotes from everything:

See below:

Code:
cd /opt/app/abc/
abcversion=abc* (abcga5 is inside /opt/app/abc/)
echo $abcversion

echoresult:

Code:
abcga5

Code:
export ABC_HOME=/opt/app/abc/$abcversion/

echo $ABC_HOME

echoresult:
Code:
/opt/app/abc/abc*/

# 6  
Old 07-18-2016
Remove the trailing /
Otherwise only matching directories are displayed.
# 7  
Old 07-18-2016
Scrutinizer is right when he says: "the assignment ( var=pattern* is the same as var="pattern*" )". It can be tested by adding quotes around the argument of echo:
Code:
abcversion=abc*
echo "$abcversion"

abc*

To activate wildcard expansion you should use an assignment like this:
Code:
abcversion=$(ls abc*)
echo "$abcversion"

abcga5

And also MadeInGermany is right when he says to remove the trailing slash in
export ABC_HOME=/opt/app/abc/$abcversion/(unless abcga5 is a directory).

Note: think about what the script should do when more files match the wildcard. Test it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Usage of # in assignment of variable

guys, i would like to know what does the below does. tr=`echo $bfile | cut -d"." -f4` tr=${tr#TR} i tried with assigning a value and executed second line. but after that also value of tr remains same. thanks in advance .. (1 Reply)
Discussion started by: expert
1 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Regex and variable assignment

Hi there, I really didn't want to have to waste people's time and ask this, but after 2 hours and running out of my own time I've decided to ask.. Basically I have a file in the format: word: other words; more words here; last word word: more words here; last word (etc) x 100 Where the... (3 Replies)
Discussion started by: maximus73
3 Replies

5. Shell Programming and Scripting

variable assignment problem

hi everyone pls pls help in the query below it's urgent pls -bash-3.00$ abc=deepak -bash-3.00$ a=1 -bash-3.00$ def_${a}=$abc -bash: def_1=deepak: command not found y iti is giving error ? (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

9. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: maverick80
7 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
Login or Register to Ask a Question