...
Commands, options, arguments & command line input
The Intro Unix: The Bash shell and commands section introduced the bash REPL – a Read, Eval, Print Loop that processes lines of command input. To review, a command consists of:
...
- Short (1-character) options which can be provided separately, prefixed by a single dash ( - )
- or can be combined with the combination prefixed by a single dash (e.g. ls -lah)
- Long (multi-character/"word") options are prefixed with a double dash ( -- ) and must be supplied separately.
- Many utilities have equivalent long and short options, both of which can have values.
- The short option and its value are usually separated by a space, but can also be run together (e.g. -f 2 or -f2)
- Strictly speaking, the long option and its value should be separated by an equal sign (=) according to the POSIX standard (see https://en.wikipedia.org/wiki/POSIX). But many programs let you use a space as separator also.
- Options usually come before arguments, but may also be allowed after the arguments depending on the tool.
More at: Intro Unix: The Bash shell and commands: Command options
Getting help
To learn what options and arguments a command has:
- In the Terminal, type in the command name then the --help long option (e.g. ls --help)
- Works for most Linux commands;
- 3rd party tools may use -h or -? or even /? instead
- May produce a lot of output, so you may need to scroll up quite a bit or pipe the output to a pager (
- e.g. ls --help | more
- Works for most Linux commands;
- Use the built-in manual system (e.g. type man ls)
- This system uses the less pager (space advances the output by one screen/"page"; typing q exits the display)
- Ask the Google, e.g. search for ls man page
- Can be easier to read
- Consult our Intro Unix: Some Linux commands wiki page
- It lists many useful Linux commands along with some of their commonly used options
More at:
Literal characters and metacharacters
...
We'll be emphasizing the different metacharacters and their usages – which can depend on the context where they're used – both in the bash command line and in commands/programs called from bash.
More at:
Command line history and editing
...
For how to edit text on the command line, see: Intro Unix: The Bash shell and commands: Command line history and editing
Tab key completion
Hitting Tab when entering command line text invokes shell completion, instructing the shell to try to guess what you're doing and finish the typing for you. It's almost magic!
...
- In more jaberwocky.txt, it is the more command that reads the file and performs its operations
- displays some data on standard output, waits for a space on standard input, repeat until no more file data
- since more is reading the file, it can display progress information about how much data has been read
- In cat jabberwocky.txt | more, the cat program reads the file data and writes it to its standard output.
- the pipe operator ( | ) then connects the standard output from cat to standard input of the more command
- the more command then reads its input from standard input, instead of from a file
- and causes the cat program to block – stop writing data to its standard output until requested
- more displays some data on standard output, waits for a space on standard input, then requests more input
- repeat until no more data from cat.
- since the data coming in to more is from an anonymous pipe, more cannot display progress information
More at: Intro Unix: Viewing text in files: Standard streams and piping
echo, head, tail, cat -n, wc
...
Code Block | ||
---|---|---|
| ||
head -n 5 haiku.txt # display the 1st 5 lines of "haiku.txt" cat -n haiku.txt # display "haiku.txt" contents with line numbers cat -n haiku.txt | tail -n 7 # display the last 7 lines of "haiku.txt" cat -n haiku.txt | tail -n +7 # display text in "haiku.txt" starting at line 7 cat -n haiku.txt | tail -n +5 | head -3 # display the middle stanza of "haiku.txt" wc -l haiku.txt # count lines in "haiku.txt" file cat haiku.txt | wc -l # use wc -l to count lines of piped-in text echo 'Hello world!' | wc -c # count characters output by echo, including the trailing newline echo -n 'Hello world' ! wc -c # count characters output by echo, without the trailing newline echo -e "aa\nbb\ncc" # output 3 lines of text using \n to represent newlines |
More at:
- Intro Unix: Viewing text in files: head and tail
- Intro Unix: Viewing text in files: Text lines and the terminal
- Intro Unix: Writing text: echo - the bash print function
Other shell concepts
Environment variables
...
Your built-in environment variables (e.g. $USER, $MY_GROUP, $PATH) and their values can be viewed with the env command.
More at: Intro Unix: Writing text: Environment variables
Quoting in the shell
When the shell processes a command line, it first parses the text into tokens ("words"), which are groups of characters separated by whitespace (one or more space characters). Quoting affects how this parsing happens, including how metacharacters are treated and how text is grouped.
...
Code Block | ||
---|---|---|
| ||
date # Calling the date command just displays date/time information echo date # Here "date" is treated as a literal word, and written to output echo `date` # The date command is evaluated and its output replaces the command today=$( date ); echo $today # environment variable "today" is assigned today's date today="Today is: `date`"; echo $today # "today" is assigned a string including today's date |
More at: Intro Unix: Writing text: Quoting in the shell
Redirection
So far text we've been working with output to standard output, which I keep reminding you is mapped to your Terminal. But you can redirect text elsewhere.
...
Note that the > redirection metacharacter sends its output to a file, not to another program's standard input stream as with the | pipe metacharacter. (There are some cases where redirection involves something other than a file, but that's a topic for the Advanced Bash scripting class.)
More at: Intro Unix: Writing text: Redirection
Errors, output and their streams
...
Code Block | ||
---|---|---|
| ||
ls haiku.txt xxx.txt # displays both output and error text on the Terminal ls haiku.txt xxx.txt 2>/dev/null # displays only output text on the Terminal ls haiku.txt xxx.txt 1>/dev/null # displays only error text on the Terminal # And this syntax (2>&1) sends standard output to outerr.log and standard error to the # same place as standard out. So data from both standard output and standard error # will be written to outerr.log ls haiku.txt xxx.txt 1>outerr.log 2>&1 |
More at:
- Intro Unix: The Bash shell and commands: Command input errors
- Intro Unix: Writing text: The standard error stream
File systems, files, and file manipulation
Let's review Intro Unix: Files and File Systems. The most important takeaways are:
- Understanding the tree-like structure of directories and files in the file system hierarchy
- Absolute paths start with a slash ( / ), the root of the file system hierarchy
- More at: Intro Unix: Files and File Systems: The file system hierarchy
- Absolute paths start with a slash ( / ), the root of the file system hierarchy
- Knowing how to navigate the file system using the cd (change directory) command, Tab key completion, and relative path syntax:
- use the dot ( . ) metacharacter for the current directory
- use the dot-dot ( .. ) metacharacters for the parent directory
- More at:
- Selecting multiple files using pathname wildcards (a.k.a. "globbing")
- asterisk ( * ) to match any length of characters
- brackets ( [ ] ) match any character between the brackets, including hyphen ( - ) delimited character ranges such as [A-G]
- braces ( { } ) enclose a list of comma-separated strings to match (e.g. {dog,pony})
- More at: Intro Unix: Files and File Systems: Pathname wildcards
- A basic understanding of file attributes such as
- file type (file, directory)
- owner and group
- permissions (read, write, execute) for the owner, group and everyone
- More at: Intro Unix: Files and File Systems: File attributes
- Familiarly with basic file manipulation commands (mkdir, cp, mv, rm)