Print (echo) variable in a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print (echo) variable in a single line
# 1  
Old 03-28-2012
Print (echo) variable in a single line

Hi,
I have written this code
------------------------------------------------
Code:
# !/bin/ksh
i=0
while [ $i -ne 10 ]
do
 j=$i
 while [ $j -ge 0 ]
 do
    echo  -e  $j
   #printf "%d",$j
    j=`expr $j - 1`
 done
  echo
 i=`expr $i + 1`
done

----------------------------------------------------
The ouput which i get is
Code:
-n 0
-n 1
-n 0
-n 2
-n 1
-n 0
-n 3
-n 2
-n 1
-n 0
-n 4
-n 3
-n 2
-n 1
-n 0
-n 5
-n 4
-n 3
-n 2
-n 1
-n 0
-n 6
-n 5
-n 4
-n 3
-n 2
-n 1
-n 0
-n 7
-n 6
-n 5
-n 4
-n 3
-n 2
-n 1
-n 0
-n 8
-n 7
-n 6
-n 5
-n 4
-n 3
-n 2
-n 1
-n 0
-n 9
-n 8
-n 7
-n 6
-n 5
-n 4
-n 3
-n 2
-n 1
-n 0

----------------------------------------
------------------------------------------

I want to print the ouput as
Code:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
and so on till
9 8 7 6 5 4 3 2 1 0

Please help !!Smilie

Last edited by Franklin52; 03-28-2012 at 10:43 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-28-2012
Code:
# !/bin/sh
i=0
while [ $i -ne 10 ]
do
    j=$i
    while [ $j -ge 0 ]
    do
        echo -e "$j \c"
        j=`expr $j - 1`
    done
    echo
    i=`expr $i + 1`
done

Instead of invoking 'expr', try using ((j--)) and ((i++))
# 3  
Old 03-28-2012
Code:
[root@dist unix]# cat a.sh 
#!/bin/bash
n=0
for i in {1..9};do
    n=${i}" "${n}
    echo ${n}
done 
[root@dist unix]# bash a.sh 
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

4. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

5. Shell Programming and Scripting

Print in single line

HI I am having a flle like below . test.txt ssh aaaaaaa@bbbbbbbbb "head -1 /xxxxxx/yyyyyyy/yyyyyyy.sp.issuer_upd_fd" echo /xxxxxx/yyyyyyy/yyyyyyy.sp.issuer_upd_fd ssh aaaaaaa@bbbbbbbbb "head -1 /xxxxxx/yyyyyyy/yyyyyyy.sp.xreff_upd_gm" echo /xxxxxx/yyyyyyy/yyyyyyy.sp.xreff_upd_gm ... (3 Replies)
Discussion started by: ptappeta
3 Replies

6. Shell Programming and Scripting

Find 2 expressions then print in a single line

Guys, need help. I have a file that contains something like this: abc def ghi jkl I want to print the first and last line of the file and the output should be in a single line. so, output should be like this: abc jkl (3 Replies)
Discussion started by: solidhelix08
3 Replies

7. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

8. Shell Programming and Scripting

echo/print variable question

while read filer ; do echo $filer $1 $2; ssh $filer vfiler status -r | awk '/running/{host=$1}/Path:/{path=$2;print host,path}'; done < filers.list this will print node1 vfiler0 / vfiler2 /vol/vfiler2_vol0 vfilert /vol/vfiler_vol vfilert /vol/virt_vol where node1 = $filer. however how... (1 Reply)
Discussion started by: riegersteve
1 Replies

9. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

10. Shell Programming and Scripting

How to print the output in single line

Hi, Please suggest, how to get the output of below script in single line, its giving me in different lines ______________________ #!/bin/ksh export Path="/abc/def/ghi"; Home="/home/psingh/prat"; cd $Path; find $Path -name "*.C#*" -newer "abc.C#1234" -print > $Home cat $Home | while... (1 Reply)
Discussion started by: Prat007
1 Replies
Login or Register to Ask a Question