...
Other programming languages usually supply a print statement or function that can direct text to files a file or to standard output.
In bash, the print utility directs text to actual printers, so there is a different method of text output: the echo command. Let's give it a try:
...
Code Block | ||
---|---|---|
| ||
echo hello # displays "hello" on the next terminal line, then the prompt on a new line
echo -n hello # displays "hello" on the next terminal line followed by the prompt (no newline) |
Exercise 3-1
What is the difference in character count when you echo hello with and without the -n option?
...
There are a number of pre-defined environment variables in the shell, such as USER (your account name) and PATH (more on PATH later). The env command will list them all along with their values.
Exercise 3-2
...
Expand | ||
---|---|---|
| ||
Use env to see your built-in environment variables. You may want to pipe output to grep -i, or less -I then search for "group" ignoring case (/group in less). |
Expand | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
Examining the env output we find that the variable MY_GROUP contains our Unix group.
|
...
- single quoting (e.g. 'some text') – this serves two purposes
- it groups together all text inside the quotes into a single token
- it tells the shell not to "look inside" the quotes to perform anyevaluationsevaluation
- all metacharacters inside the single quotes are ignored
- in particular, any environment variables in single-quoted text are not evaluated
- double quoting (e.g. "some text") – also serves two purposes
- it groups together all text inside the quotes into a single token
- it allows environment variable evaluation, but inhibits some metacharcters
- e.g. asterisk ( * ) pathname globbing (more on globbing later...)
- and some other metacharacters
- backtick quoting (e.g. `date`)
- evaluates the expression inside the backtick marks ( ` )
- the standard output of the expression replaces the text inside the backtick marks ( ` )
...
The first rule of quoting is: always enclose a command argument in quotes if it contains spaces so that the command sees the quoted text as one item. See the difference between:
Code Block | ||
---|---|---|
| ||
echo 'Hello world!' # the argument "Hello world!"
|
To see more on how quoting affects text grouping, we'll use quotes to define some multi-word environment variables.
Tip |
---|
Always use single ( ' ) or double ( " ) quotes when you define an environment variable whose value contains spaces. |
See the difference between:
Code Block | ||
---|---|---|
| ||
foo="My name is Anna"'Hello world' # correct - defines variable "foo" to have value "Hello world" foo=Hello world # error - no command called "world" |
The 2nd expression above, without the quotes, produces an error. What's going on? The shell parses the input into two tokens: "foo=Hello" and "world". It assigns the value "Hello" to the variable foo, then tries to execute world, which it thinks is a command.
As for the difference between single quotes and double quotes, these two expressions produce the same output because the assigned text does not contain any special metacharacters:
Code Block | ||
---|---|---|
| ||
foo="My name is Anna"; echo $foo
foo='My name is Anna'; echo $foo
|
...
But these two expressions are different:
...
- To delete text after the cursor, use:
- Delete key on Windows
- Function-Delete keys on Macintosh
- To delete text before the cursor, use:
- Backspace key on Windows
- Delete key on Macintosh
- Use Ctrl-k (kill) to delete everything on the line
- This is different from Ctrl-k on the command line where it deletes everything after the cursor
Once you're satisfied with your edits:
...