Search code examples
bashsyntaxbackticks

Why does bash return the file directory when using a back tick?


I'm running this series of commands

passwd=`wc -l /etc/passwd`

echo $passwd

Returns:

34 /etc/passwd

What do I need to do to this so that it will only show the output of wc -l?


Solution

  • Using awk perhaps ?

    $ passwd=$(wc -l /etc/passwd | awk '{print $1}')
    $ echo $passwd
    32
    

    Using cut, from cut (GNU coreutils)

    $ passwd=$(wc -l /etc/passwd | cut -d" " -f1)
    $ echo $passwd
    32