I don’t understand what a single backtick and double quote and >> do in this script Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of I don’t understand what a single backtick and double quote and >> do in this script without wasting too much if your time.

The question is published on by Tutorial Guruji team.

This example was in a Linux book:

$ cat sort-wc
#!/bin/bash
# Sort files according to their line count
for f
do
    echo `wc -l <"$f» lines in $f
done | sort -n
$ ./sort-wc /etc/passwd /ect/fstab /etc/motd

What I don’t get is why there is only a single backtick, a single double quote and what the >> does. Isn’t >> for writing to a file?

Answer

This is from page 121 of “Introduction to Linux for Users and Administrators” and that’s a typographical error in the text. The script is also avaliable in other texts from tuxcademy, with the same typographical error.

The single » character is not the same as the double >> and it serves no purpose in a shell script. My guess is that the typesetting system used for formatting the text of the book got confused by "` for some reason and formatted it as a guillemet (angle-quote), or it’s just a plain typo (the «...» quotes are used for quoting ordinary text elsewhere in the document).

The script should read

#!/bin/bash
# Sort files according to their line count
for f
do
    echo `wc -l <"$f"` lines in $f
done | sort -n

… but would be better written

#!/bin/sh
# Sort files according to their line count

for f; do
    printf '%d lines in %sn' "$(wc -l <"$f")" "$f"
done | sort -n

The backticks are an older form of $( ... ), and printf is better to use for outputting variable data. Also, variable expansions and command substitutions should be quoted, and the script uses no bash features so it could just as well be executed by /bin/sh.

Related:

We are here to answer your question about I don’t understand what a single backtick and double quote and >> do in this script - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji