[Tip] A better echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Tip] A better echo
# 1  
Old 12-17-2019
[Tip] A better echo

Often it has been said that echo is neither portable nor correct.
Here is an input.txt:
Code:
line1
line2
-n
line4
-en
line6
-x
line8

Then the following fails with BSD/Linux/bash:
Code:
while IFS= read line
do
  echo "$line"
done < input.txt

It is elegantly improved by means of an echo function:
Code:
echo_(){
  ( IFS=" "; printf "%s\n" "$*" )
}

while IFS= read line
do
  echo_ "$line"
done < input.txt

(Of course you can directly use the printf instead of the echo function.)
Further it provides compatibility with ksh/sh/psh/ash/dash, and even an old Bourne shell might work.

The following is a suite of echo functions that lets you easily go into a script and replace
  • echo with echo_
  • echo -n with echo_n
  • echo -e with echo_e
  • echo -ne or echo -n -e with echo_ne
  • echo -en or echo -e -n with echo_ne (or echo_en)



Code:
# Simple echo
echo_(){
  ( IFS=" "; printf "%s\n" "$*" )
}

# Portable echo -n
echo_n() {
  ( IFS=" "; printf "%s" "$*" )
}

# Portable echo -e
echo_e() {
  ( IFS=" "; printf "%b\n" "$*" )
}

# Portable echo -ne
echo_ne() {
  ( IFS=" "; printf "%b" "$*" )
}

alias echo_en=echo_ne


Last edited by MadeInGermany; 12-18-2019 at 08:25 AM.. Reason: Fixes, thanks to Scrutinizer
These 3 Users Gave Thanks to MadeInGermany For This Post:
# 2  
Old 12-17-2019
Hi MadeInGermany,

Can't you use:
Code:
echo_e() {
  printf "%b\n" "$*" 
}

Instead?

The use of "$*" means that this only works as long as IFS is at the default:

Code:
$ IFS=,
$ echo -e "a\nb" c d
a
b c d
$echo_e "a\nb" c d
a
b,c,d

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-18-2019
SmilieTwo times thank you, Scrutinizer!Smilie
I didn't yet discover the %b or thought it's a GNUism. But seems to be standard.
Now I set IFS=" " in a sub shell.
I have implemented the fixes in my original post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Tip: Ldom migration

Prerequisite: • S7-2L Server Hardware Console Access • Solaris 11.3 OS and LDOM Packages (Ex: 3.4) • Setup IPS Repositories Solaris 11 comes with Oracle VM server pre-installed if older version is there remove the old and install latest Oracle VM... (1 Reply)
Discussion started by: mahendra170
1 Replies

2. Shell Programming and Scripting

Search tip.

How do I find a key word in multiple files.. in a directory.. ? cat *.doc | grep -i myword? (7 Replies)
Discussion started by: hamon
7 Replies

3. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

4. Shell Programming and Scripting

Regexp tip

Hello, I'm just starting working on it. I'd like to get a tip For istance if I have a file like: a b c d e f .... and I wanna get: 1a & 2b & 3c 0d & 8e & 4f ..... I would like to use sed and come up with a regular expression that works.... (3 Replies)
Discussion started by: Dedalus
3 Replies

5. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

6. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

7. Solaris

Solaris; tip

plz explain TIP in solaris in detail. (11 Replies)
Discussion started by: karman0931
11 Replies

8. Shell Programming and Scripting

Little bit of a help or just a tip

I am about to do a script that change the COST so i dont need to change each cost. The output looks like this. "OL_ID OL_LINK_COST ----------- ------------ 51 10 52 10 53 10 54 10 55 ... (3 Replies)
Discussion started by: maskot
3 Replies

9. Solaris

tip into 280R

I need to use tip from machine A serial port to machine B serial port. Can someone point me to an example of the correct cable to use? Thanks. (1 Reply)
Discussion started by: dangral
1 Replies

10. UNIX for Dummies Questions & Answers

one teaching Tip

Student have huge interest about why so many expert choose use UNIX than MS Windows. I consider that SHARE & OPEN is key point.:) (2 Replies)
Discussion started by: 111000
2 Replies
Login or Register to Ask a Question