Echo with ksh shell


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Echo with ksh shell
# 1  
Old 01-11-2008
Echo with ksh shell

Hi All,
I'm trying to use the "echo" command in a korn shell script, and I want it to drop the trailing newline. Now I know that with the bash shell, the "-n" flag would solve this issue. Does anyone know how this can be done with the korn shell?

Cheers
Khoom
# 2  
Old 01-11-2008
From man ksh

Code:
       echo [-neE] [arg ...]
              Prints its arguments (separated by spaces) followed by  a  new-
              line, to standard out.  The newline is suppressed if any of the
              arguments contain the backslash sequence \c.  See print command
              below  for  a list of other backslash sequences that are recog-
              nized.

              The options are  provided  for  compatibility  with  BSD  shell
              scripts:  -n  suppresses the trailing newline, -e enables back-
              slash interpretation (a no-op, since this  is  normally  done),
              and -E which suppresses backslash interpretation.

Did you try with -n ?
# 3  
Old 01-11-2008
<Sigh> Vino,

In a ksh script, the line

echo -n "hi I'm happy"


will give the output

-n hi I'm happy
# 4  
Old 01-11-2008
Code:
[/tmp]$ cat try.ksh
#! /bin/ksh

echo -n "test"
[/tmp]$ ./try.ksh
test[/tmp]$

# 5  
Old 01-11-2008
In Korn, a simple option is to use print or printf.

Code:
     print [ -Rnprsu[n ] ] [ arg ... ]

         The shell output mechanism. With no flags or with flag -
         or  -,  the  arguments are printed on standard output as
         described by echo(1). The exit status is 0,  unless  the
         output file is not open for writing.

# 6  
Old 01-11-2008
I'd like to second reborg: for the umpteenth time now: if in ksh, use "print" AND NOT "echo"! "print" is a shell-built-in in ksh and therefore preferable over an external command like "echo".

bakunin
# 7  
Old 01-11-2008
You need to state which ksh version is being and which ksh man page you are looking at. The hp-ux ksh man page says:

"echo [arg ...]
See echo(1) for usage and description."

and the echo man page has no -n mentioned. Instead you use \c. This is the System V version of echo and is the most common in ksh. But ksh can be built with other version of echo. If this is done, it violates the Posix standard which mandates the System V echo behavior.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

please explain why this ksh echo deletes character

Please explain why this works. Am unable to find another definition for the '%', which would explain this behaviour: spaceLeft=`df -h /myPartition | tail -1` # output looks like: /dev/sda5 10G 1.2G 12G 29% / set -- $space #this deletes the trailing '%' sign, which is... (6 Replies)
Discussion started by: frododot
6 Replies

2. Shell Programming and Scripting

\n in ksh using echo & printf

#!/usr/bin/ksh var1="Hi World" var2="Morning" var3=$(echo "$var1" \n "$var2") echo $var3 var3=$(printf "$var1 \n $var2") echo $var3 Output Any way to get in my $var3 ? (7 Replies)
Discussion started by: dahlia84
7 Replies

3. Shell Programming and Scripting

shell echo help needed

echo '#!/usr/local/bin/expect' > sree_expt echo "spawn passwd $User" >> sree_expt echo 'expect "New password:"' >> sree_expt echo send "$Password\r" >> sree_expt echo 'expect "Re-enter new password:"' >> sree_expt echo send "$Password\r" >> sree_expt echo "expect eof" >> sree_expt for... (0 Replies)
Discussion started by: sreedhargouda
0 Replies

4. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

5. UNIX for Advanced & Expert Users

echo in ksh sh & bash

Hello, I have lib file which contain a function that get text to print on screen by echo command. Several scripts are inculde this lib and use this function. Each one of them is written in different shell language (sh ksh & bash). This causing some issues when using backslash charater as... (4 Replies)
Discussion started by: Alalush
4 Replies

6. Shell Programming and Scripting

ksh - how to echo something in color and bold

Hi all, I was to echo Hi in Red and Bold ; and echo There is in Green and bold I got bold to working using tput bold but i am having hard time getting the color. Any help is appreciated, jak (4 Replies)
Discussion started by: jakSun8
4 Replies

7. Shell Programming and Scripting

Shell and echo

Probably my first post, very new to shell scripting :) Here is the script i am trying to modify to use function # Script to create simple menus and take action according to that selected # menu item # while : do clear echo "-------------------------------------" echo "... (2 Replies)
Discussion started by: replyramdas
2 Replies

8. Shell Programming and Scripting

what is ksh equivalent of bash echo -n ?

Hi folks, I need to stop printing a new line after echoing a string in KSH. i know bash provides echo -n "string" what is the ksh equivalent for this ? (3 Replies)
Discussion started by: mudhireddy
3 Replies

9. Shell Programming and Scripting

Perl equivalent of ksh if / echo statement

Is there an equivalent perl statement for the following ksh statement ? example if then ... else ... fi (2 Replies)
Discussion started by: gefa
2 Replies

10. UNIX for Dummies Questions & Answers

echo $SHELL, $PWD and etc.

hi, this echo $SHELL will give the shell name.. how to get the other list of variables (besides SHELL) values? and also, different shells have different variable names (example SHELL) (10 Replies)
Discussion started by: yls177
10 Replies
Login or Register to Ask a Question