A simple bash script that mimics the functionality of the Unix/Linux wc (word count) command.
This script counts the number of lines, words, and bytes in text, either from a file or from standard input.
- Counts lines, words, and bytes
- Accepts input from a file or standard input (stdin)
- Returns formatted output similar to the standard
wccommand - Provides error handling for non-existent files
./wc_script.sh filename.txtThis will output the line count, word count, byte count, and the filename.
cat filename.txt | ./wc_script.shor
echo "Hello World" | ./wc_script.shThis will output just the line count, word count, and byte count.
- For file input:
[lines] [words] [bytes] [filename] - For stdin input:
[lines] [words] [bytes]
$ ./wc_script.sh sample.txt
5 20 120 sample.txt
$ echo "Hello World" | ./wc_script.sh
1 2 12- Save the script to a file (e.g.,
wc_script.sh) - Make it executable:
chmod +x wc_script.sh
- Run it as shown in the usage examples above
- Bash shell
- Standard Unix utilities (
cat,wc)