Bash Commands and Keywords
The building blocks that make-up Bash scrips are Commands, keywords, keys and special characters.
Commands to navigate through file systems you may have very likely seen before. Many commands have options associated with them. You can access these options via -option_name.
-
-
- ls -a List all files and folders
- ls <foldername> List files in a folder
- ls -lh Detailed list with a human-readable output
- ls -l *.jpg List only jpeg files
- ls -lh <fileName> Result for only a single file
- cd <foldername> Change directory – If the folder name has spaces, use quotes. Example: cd “Folder With Spaces”
- cd .. Go up one folder.
- du -h Disk usage of folders in a human-readable output
- du -ah Disk usage of files and folders in a human-readable output
- du -sh Only show disk usage of folders
- pwd Print the working directory
- man <command> Shows the manual for a command or program
-
Special characters come in handy when trying to match patterns in files such as an example above where ls -l *.jpg will only return jpg files. The * character acts as a wildcard standing in for an undefined string. * will match any number of characters if you only want a single character to be wild use ? instead or [ ] for start and end character sets respectively.
Other important special characters include:
-
-
- ~ Points to the home directory
- # Insert a comment
- & Run as a background job
- ‘ Strong Quote
- ” Weak quote
-
To avoid unexpected results from your bash scripts make sure you understand the difference between strong and weak quotes.
Use the commandcomplete -A keyword :
followed by : Space Tab Tab to get a list of all the shell keywords.
Finally, terminals send characters (bytes), not keystrokes. Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters.
Control characters are not printable, they display in the terminal as ^ and are intended to have an effect on applications.