1

I am trying to print two outputs on a single line like this:

4 3

But I am getting this:

4
3

These are my two commands:

find "$1" -type f | wc -1
find "$1" -type d | wc -1 

I have tried assigning each one to a variable and then using echo or printf to combine them, but nothing seems to be working.

lit
  • 14,456
  • 10
  • 65
  • 119
  • "I have tried assigning each one to a variable and then using echo or printf to combine them." That should work. What exactly did you try and what problem did you have? – rici Sep 07 '18 at 04:00
  • 2
    Possible duplicate of [Concatenate in bash the output of two commands without newline character](https://stackoverflow.com/questions/20871534/concatenate-in-bash-the-output-of-two-commands-without-newline-character) – Sangam Belose Sep 07 '18 at 05:11

3 Answers3

1

I could not reproduce your problem.

var1=`find "." -type f | wc -l`
var2=`find "." -type d | wc -l`
echo $var1 $var2
8514 93

But still if you want to replace any \n characters from input, do following - Use tr to replace any newline in your input.

For example,

echo -e "4\n3" Outputs

4

3

echo -e "4\n3" | tr '\n' ' ' (replace newline with space)

outputs

4 3

Alternatively, you could add tr '\n' '' (replace newline with nothing) before assigning the value to a variable.

Example:

var1=`find "$1" -type f | wc -1 | tr '\n' ''`
var2=`find "$1" -type d | wc -1 | tr '\n' ''`

echo "$var1 $var2"
Community
  • 1
  • 1
xxx374562
  • 226
  • 1
  • 8
  • Please don't use backticks \`, backticks are deprecated and `$( .. )` are __greatly__ preferred. – KamilCuk Sep 07 '18 at 07:54
  • 1
    @KamilCuk thanks, I did not know. I read https://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated now – xxx374562 Sep 07 '18 at 07:57
1

You should list your complete example, with the echo commands you used.

The answer by Roger That should work.

You may also consider using

  • echo -n , e.g:

    var1="3"
    var2="4"
    echo -n "$var1"
    echo -n " "
    echo "$var2"
    

    will produce

    3 4
    
  • printf, which enables formatting, e.g:

    var1="3"
    var2="4"
    printf "%d %d\n" $var1 $var2
    

    will output

    3 4
    
user1551605
  • 166
  • 9
  • Nice, I learned a flag for echo that will prevent me from using `printf` so much. Any reason not to use `printf` is a good reason. : p – bballdave025 Sep 21 '18 at 01:50
0

This is one of those things that comes from lots of experience. You were on the right track by looking at echo and printf.

I'm going to start by using command substitution, because it's the first method that comes to mind. I'll combine it with echo. (It's that experience thing again.) You can read up about command substitution in lots of places, try Googling it or check out the question and answers here.

Our goal can be stated as performing these steps:

#Not real code
$ echo "<number_of_files> <number_of_directories>"

Command substitution simply allows us to use the output of a command to give us <number_of_files> and <number_of_directories>. To get these outputs, we use the pattern $(my_command), which will give whatever output that running

$ my_command

at the terminal gives. Hopefully this will become clearer when you get to the part I've marked by putting the words, "Here we do the command substitution", in bold.

I'm going to imagine that you've got a directory structure something like what follows. (I call it no_stack_dir because you don't want your numbers stacked one on top of the other.)

no_stack_dir/
├── dir1
├── dir2
├── file1
├── file2
├── file3
└── file4

I'm going to simplify things by not creating a script. Instead of using "$1", I'm just going to use a directory name -- in this case /home/jsmith147/no_stack_dir. Before I show the method, I hope to ensure that our directory setup is understood by using the following commands.

$ cd ~

$ pwd
/home/jsmith147

$ls
no_stack_dir

$ls no_stack_dir
dir1    file2
dir2    file3
file1   file4

Notice I have only 2 subdirectories, because our find command will include no_stack_dir in its found directories along with dir1 and dir2.

Here we do the command substition, combined with echo.

Our <number_of_files> comes from the command,

find /home/jsmith147/no_stack_dir -type f | wc -l

and our <number_of_directories> comes from the command,

find /home/jsmith147/no_stack_dir -type d | wc -l

Note that I didn't put the terminal prompt, $ before those commands. Here is where command substitution comes in. We go from

#Not real code
$ echo "<number_of_files> <number_of_directories>"

to

#Real code
$ echo "$(find /home/jsmith147/no_stack_dir -type f | wc -l) $(find /home/jsmith147/no_stack_dir -type d | wc -l)"

I hope I've made it clear enough with these examples so that you can write a script (or something else that uses "$1") that would be run something like

$ ./no_stack.sh /home/jsmith147/no_stack_dir

Feel free to ask questions. There are many other ways to accomplish what you are trying to do. Perhaps the SO community can come up with a bunch of other ways to produce the same output. It would be interesting to list them here.

bballdave025
  • 1,347
  • 1
  • 15
  • 28