Echo with ksh shell


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

In my version of ksh (pdksh), echo is a builtin, and it knows about "-n":
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate ksh, pdksh builtins, echo -n.

echo
echo "(Versions displayed with local utility \"version\")"
version && version ksh

echo
echo " This process is:"
ps | grep $$

do_cmd()
{
  echo
  CMD="$1"
  if ! command -v $CMD >/dev/null
  then
    echo " Command $CMD not found."
        return 1
  else
    command -V $CMD
        if ! builtin $CMD
        then
          echo " Command \"$CMD\" is not a builtin."
        fi
  fi
  shift
  builtin $CMD $*
}

do_cmd junk trash
do_cmd cat /dev/null
do_cmd print " Hello, world from ksh builtin \"print\""
do_cmd echo " Hello, world from ksh builtin \"echo\""

echo
echo " Testing echo -n:"
echo -n " No newline, "
echo -n " not here, either, "
echo -n " and certainly not here, "
echo " but here is one."

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
pdksh 5.2.14 99/07/13.2

 This process is:
19561 pts/2    00:00:00 ksh

 Command junk not found.

cat is a tracked alias for /bin/cat
./s1[33]: builtin: cat: not a builtin

print is a shell builtin

Hello, world from ksh builtin "print"

echo is a shell builtin

Hello, world from ksh builtin "echo"

 Testing echo -n:
 No newline,  not here, either,  and certainly not here,  but here is one.

There is a long passage in man ksh about special, regular, etc., commands, comparing the original ksh to the POSIX version. Perhaps that is causing the apparent anomaly -- I readily admit that using pdksh is not proof of anything in ksh.

I generally go by the guideline that less is better: "echo" is shorter than "print" Smilie, although there are cases when printf is very useful.

Best wishes ... cheers, drl
# 9  
Old 01-11-2008
Actually, I have never seen any version of ksh that does not have a version of echo built in. Dave Korn intended that the builtin echo would mirror the behavior of the external command echo on any OS. He provided the print command so that portable ksh scripts could be written. Then came Posix which standardized the echo command. Posix does not recognize print but does recognize printf. printf tends to not be a builtin among the various versions of ksh. The print command supports the -u option and you can't use co-processes without it. Personally I use echo and expect a Posix compliant echo. And I use print when echo won't do. Because printf tends to be an external command, I resist it, but it is very powerful and, sometimes, it is worth the cost.
# 10  
Old 01-11-2008
In AIX, the ksh88 "echo" is a builtin and uses "\c" but not "-n". Same for ksh93, and same for /bin/echo.

On MacOSX, ksh rules are reversed (uses "-n" but not "\c"). However, /bin/echo will happily accept either form.

It looks like you need to balance your portability needs against the speed/elegance of code. Apparently the ony way to be sure of cross-platform success is to use printf, which is an external program. (You could also use perl, or awk, but...gads.)

The echo builtins and /bin/echo should be equivilant, although they are not always!

Stupid POSIX. A laudable goal but it sure seems to dumb things down. When can we go to POSIX 2.0? Smilie
# 11  
Old 01-17-2008
I agree with one of the answers above:
use print
You should use the following syntax:
print -n -- "...."
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