Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have done this little 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? Thank you in advance ;)

share|improve this question

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
    
Thank you for your response, but $size isn't printed (there is a blank instead). –  user3581976 Jul 13 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 at 13:28
    
It outputs "Size is 12"... –  user3581976 Jul 13 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 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 at 13:40

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.

share|improve this answer

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"
share|improve this answer
    
wc will still output a newline this way. –  goldilocks Jul 13 at 13:16
    
Yes, use @goldilock's method to suppress newline from wc –  unxnut Jul 13 at 13:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.