top of page
Search
  • Writer's pictureXin Li

Bash Shell Command Reference

Updated: Feb 17, 2020

A quick reference of useful bash shell commands.


Note: on the Windows operating system, an ideal tool to test the commands would be Git Bash. On MacOS or Linux, the default terminal would work.


 

Printing Messages: echo


This is the command to get the shell to print messages. One thing to notice is that some characters have special meanings in the shell, like the exclamation point (!), a straightforward way to resolve this is by surrounding the message using straight quotes ('), not curly quotes though, it doesn't work.


Demo


Another notable thing is the "$" symbol, a "$" symbol in front of a word indicates a shell variable, for instance, to display the current shell window / terminal size:

 

Searching and Pipes: grep, wc


Introduction


  • grep is short for "global regular expression print", which processes text line by line and prints any lines with a match to the specified pattern.

  • wc is short for "word count" which reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count.

Demo


To search for a word in a file, use the following command:


However, for a result to big to display in one screen, the terminal will be filled with the results and unable to display them completely:

To resolve this issue, we can use the pipe operator '|' along with the less operator as we previously introduced:

We can see the result of the command grep shell dictionary.txt is piped to the less program.

The grep command could also used in combination of other commands with piping:

If we would like to know how many matches there are, we can use the wc operator with -l for number of lines (matched words in our example) or give the grep operation -c for count:

Regular Expressions


grep also uses regular expressions to specify patterns for matching:

The expression "[Vv]alenti*" matches everything containing either "valenti" or "Valenti".


Common Options


There are also some other handy options that come with grep:

-i: ignore case

-v: exclude matched lines

-n: display line numbers, which is great for locating matches in the original file

Here -vn is a combination of -v and -n operators which exclude the matched lines from the document as well as show the original line numbers as the lines located in the document:


60 views0 comments

Opmerkingen


bottom of page