Kieran Lavelle
Kieran Lavelle

Reputation: 446

Why is the result of my varible being output in terminal. Bash

#!/bin/bash

main (){
    totalnumber
    totalfiles
}

totalnumber (){
    TNUM= ls -1 | wc -l
}

totalfiles (){
    FNUM= ls -l | grep ^d | wc -l
}

main

This is my script, whenever I run it however in a folder with 2 files and 2 it outputs 4 and 2 but I don't want these to be output in the terminal just yet. How can I stop this?

Upvotes: 1

Views: 34

Answers (2)

timbru31
timbru31

Reputation: 365

#!/bin/bash

main (){
    totalnumber
    totalfiles
}

totalnumber (){
    TNUM=$(ls -1 | wc -l)
}

totalfiles (){
    FNUM=$(ls -l | grep ^d | wc -l)
}

main

if you would

echo $TNUM
echo $FNUM

after the call of main, you will see your output.

Upvotes: 0

P.P
P.P

Reputation: 121427

Because you are not actually assigning the results with these

TNUM= ls -1 | wc -l

and

FNUM= ls -l | grep ^d | wc -l

Do:

TNUM=$(ls -1 | wc -l)

and

FNUM=$(ls -l | grep ^d | wc -l)

to assign the results.

Upvotes: 2

Related Questions