Strange Phenomena with records filed in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange Phenomena with records filed in variable
# 8  
Old 10-02-2014
Thank you Scrutinizer and Corona688.

Code:
printf "%s\n" "${some_arbitary_varible}"

... does the trick.

Btw,
Code:
print -r "${some_arbitary_varible}"

does the same in ksh.

Is there any special reason why you use
Code:
"${some_arbitary_varible}"

instead of
Code:
"$some_arbitary_varible"

?
# 9  
Old 10-02-2014
Quote:
Originally Posted by Cochise
... ... ...
Is there any special reason why you use
Code:
"${some_arbitary_varible}"

instead of
Code:
"$some_arbitary_varible"

?
In this case, they are synonymous. The braces must be used if the character following the variable name is a valid character in a variable name and when referencing a multi-digit positional parameter. Some people always use braces around shell variable names to emphasize which characters are in the variable name.

Note that both:
Code:
${1}1
$11

expand to the contents of the 1st positional parameter followed by the character "1" (even if there are eleven or more positional parameters), but both:
Code:
${X11}
$X11

expand to the contents of the variable X11 even if X1 is defined and X11 has not been defined.
# 10  
Old 10-03-2014
Quote:
Originally Posted by Cochise
Btw,
Code:
print -r "${some_arbitary_varible}"

does the same in ksh.
ksh has printf, too. And especially, printf "%s\n" "string" works the same everywhere, while "print" requires ksh.
Quote:
Is there any special reason why you use
Code:
"${some_arbitary_varible}"

instead of
Code:
"$some_arbitary_varible"

?
What if you want to print $some_string_postfix where _postfix is not part of the variable? That will mess up. ${some_string}_postfix will work. { } are just good habits.

Last edited by Corona688; 10-03-2014 at 10:57 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace variable value in first file based on records in second

Hello , I have below files a) File A <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0"> <project name="source"> <mapping name="m_Source"> <parameter... (3 Replies)
Discussion started by: Pratik4891
3 Replies

2. Shell Programming and Scripting

Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file. I have a simple command such as below: awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt but I want x to equal the number of records in a different file, b.txt (10 Replies)
Discussion started by: Geneanalyst
10 Replies

3. Shell Programming and Scripting

Bash - concatenate string - strange variable scoping

Hello, I am trying to concatenate a string in a bash script like this: runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" " env | grep "$ENV_SUFFIX" | while read line; do envCmd="-e \"${line}\" " runCmd=$runCmd$envCmd echo $runCmd # here concatenation works fine done echo... (3 Replies)
Discussion started by: czabak
3 Replies

4. Programming

Strange value of the double type variable: -nan(0x8000000000000)

I am confused by the value of "currdisk->currangle" after adding operation. Initially the value of "currdisk->currangle" is 0.77500000000000013, but after adding operation, it's changed to "-nan(0x8000000000000)", Can anyone explain ? Thanks! The following is the occasion of gdb debugging. 3338 ... (8 Replies)
Discussion started by: 915086731
8 Replies

5. Shell Programming and Scripting

Prefix a variable in the first column of all the records of the files with and without header

In a bash shell, I have to prefix a variable to two .CSV files File1.CSV and File2.CSV. One of the files has a header and the other one is with no header in the below format: "value11","value12","value13","value14","value15","value16" "value21","value22","value23","value24","value25","value26"... (7 Replies)
Discussion started by: dhruuv369
7 Replies

6. Shell Programming and Scripting

Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test: if ( bindex < s ) "bindex" is the "index" of my "bin" (the array element that gets incremented whenever a... (2 Replies)
Discussion started by: treesloth
2 Replies

7. Shell Programming and Scripting

filter out all the records which are having space in the 8th filed of my file

I have a file which is having fileds separtaed by delimiter. Ex: C;4498;qwa;cghy;;;;40;;222122 C;4498;sample;city;;;;34 2;;222123 C;4498;qwe;xcbv;;;;34-2;;222124 C;4498;jj;sffz;;;;41;;222120 C;4498;eert;qwq;;;;34 A;;222125 C;4498;jj;szxzzd;;;;34;;222127 out of these records I... (3 Replies)
Discussion started by: indusri
3 Replies

8. Shell Programming and Scripting

Using a variable to select records with awk

As part of a bigger task, I had to read thru a file and separate records into various batches based on a field. Specifically, separate records based on the value in the batch field as defined below. The batch field left-justified numbers. The datafile is here > cat infile 12345 1 John Smith ... (5 Replies)
Discussion started by: joeyg
5 Replies

9. Shell Programming and Scripting

Using awk to search variable length records

New to awk and need some help. I have a script that I would like to make more compact. I want to read a file and grab every field, from every record, except the last field. The records are variable length and have varying number of fields. A record will have at least two fields, but can have... (9 Replies)
Discussion started by: synergy_texas
9 Replies
Login or Register to Ask a Question