6

I have done this script :

#! /bin/sh  
path="foo" 
while touch $path ; do 
    path=./${path} 
done 

echo "echec avec le nom $path qui fait" 
echo $path|wc -c 
echo "caracteres"

How can I print on the same line the 3 echo lines?

user3581976
  • 3,155

3 Answers3

10

It would be far better if you didn't use echo at all. The use of echo when combined with arbitrary input can have unintended effects. What's more, you do not need wc to count the characters in a shell variable - the shell can do it just as well and without execing a separate process to boot.

For example:

with wc:

_path=/some/place/in/my/filesystem
printf %s "$_path" | wc -c

###OUTPUT###
28

no wc:

printf %s "${#_path}"

###OUTPUT###
28

two printf arguments:

printf 'Arg char count:\t%s\nArg contents:\t%s\n' \                                                                    
    "${#_path}" "$_path"

###OUTPUT###
Arg char count: 28
Arg contents:   /some/place/in/my/filesystem

your command:

printf 'echec avec le nom %s qui fait %s caracteres\n' \
    "$_path" "${#_path}"

###OUTPUT###
echec avec le nom /some/place/in/my/filesystem qui fait 28 caracteres

See Why is printf better than echo? and the spec on POSIX parameter expansion for more information.

mikeserv
  • 58,310
7

To suppress the newline with the echo used on linux systems1, use -n:

echo -n "echec avec le nom $path qui fait"

However, wc also prints a newline, and that can't be suppressed, but it can be discarded:

size=$(echo $path | wc -c);
echo "echec avec le nom $path qui fait $((size-1)) caracteres"

I've used $((size-1)) here because wc will have counted the newline output by echo. You could instead use size=$(echo -n $path | wc -c), but beware the caveat about the non-standardness of -n.


1. The -n implemented by GNU coreutil's echo is non-standard, so YMMV. Fortunately, you don't actually need to use it here.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • Thank you for your response, but $size isn't printed (there is a blank instead). – user3581976 Jul 13 '14 at 13:24
  • You've done something wrong. Try on the commandline size=$(echo "hello world" | wc -c); echo "Size is $size" If that doesn't output Size is 12, your computer has been possessed by aliens, etc. – goldilocks Jul 13 '14 at 13:28
  • It outputs "Size is 12"... – user3581976 Jul 13 '14 at 13:31
  • But this : path="fooo" $size=$(echo $path | wc -c); echo "echec avec le nom $path qui fait $size caracteres"

    prints "echec avec le nom foo qui fait caracteres"

    – user3581976 Jul 13 '14 at 13:37
  • Don't use $ with the variable name in the declaration/assignment. I.e., size=, not $size=. You can actually do this: echo "echec avec le nom $path qui fait $(echo -n $path | wc -c) caracteres". I've thrown an -n in there because wc counts the newline otherwise, but I've also edited in another method to deal with that above (see last paragraph). – goldilocks Jul 13 '14 at 13:40
  • Note that echo will most likely be a shell builtin, not the echo from coreutils. – Dennis Jul 14 '14 at 02:32
  • wc -c counts bytes, not characters. Just as well since character encodings are not enforced for file names. If you know file names are encoded in the current locale, you can use wc -m to count the characters. – Stéphane Chazelas Jan 14 '15 at 15:30
  • With many echo implementations, that won't work properly if $path contains backslash characters (try with _path='/tmp/\ctest' with various shells/echo). – Stéphane Chazelas Jan 14 '15 at 15:31
0

The -n option with echo suppresses newline. So, you can do:

echo -n "echec avec le nom $path qui fait" 
echo -n $(echo $path|wc -c)
echo "caracteres"
unxnut
  • 6,008