LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple scripting question (https://www.linuxquestions.org/questions/programming-9/simple-scripting-question-141942/)

wedgeworth 02-03-2004 04:40 PM

simple scripting question
 
is there anyway to assign a carriage return to a variable? just i regular bash scripting.

space="\n"

just ends up printing \n in the middle of the text instead of seperating it with a carriage return.

any help?

crabboy 02-03-2004 04:46 PM

Code:

#!/bin/sh

HELLO='
'

echo "hello $HELLO there dude"

Code:

# . test.sh
hello
 there dude


wedgeworth 02-03-2004 05:00 PM

i think i tried that. or atleast something like that. anyway, i tried it like you did just to see. it works for the print out like that put when i use it in my array....which is what i've been testing most of this on before i turned here.... it didn't work. you can see the exact same variable is used for your test and mine. it works on yours but not mine. it just enters a space. not a return. why?

//***************************************\


#!/bin/bash

hi="hi"
space=" "
#space2="******"
space2='

'
goodbye="goodbye"
i=0
HELLO='
'

#Spare out entries into array
#priming read
array3[0]=${hi}$HELLO${goodbye}
echo ${array3[i]}
#echo $i


echo "hello $HELLO there dude"



//***************************************\
# ./carriage_return.sh
hi goodbye
hello
there dude

maillion 02-03-2004 10:29 PM

The echo command in bash will allow you to enter a newline or carriage return by using \n and \r respectively. You may have to use "echo -e \n" or "echo -e \r" (without the quotes). The -e is supposed to allow echo to use these 'special characters'. Try it both ways to learn...:cool:

wedgeworth 02-04-2004 08:21 AM

maillion...i understand that. the problem is assigning that value into the middle of an array of characters. that's the problem. see the code above for what i'm trying to do. i want to be able to assign a carriage return between "hi" and "goodbye" in the array3[0] array.....sorry if i'm missunderstanding. i just wanted you to know what i was trying to do.

crabboy 02-04-2004 10:01 AM

Using the -e with echo works.
Code:

#!/bin/bash

hi="hi"
space=" "
#space2="******"
space2='

'
goodbye="goodbye"
i=0
HELLO='\n'

#Spare out entries into array
#priming read
array3[0]=${hi}${HELLO}${goodbye}
echo -e ${array3[i]}
#echo $i


echo -e "hello $HELLO there dude"


wedgeworth 02-04-2004 10:30 AM

sweet. now i under stand. thnx


All times are GMT -5. The time now is 03:50 PM.