...
- single quoting (e.g. 'some text') – 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 any evaluation
- all metacharacters inside the single quotes are ignored
- in particular, any environment variables in single-quoted text are not evaluated
- single quoting preserves any whitespace present in the text (spaces or newlines)
- 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 and some other metacharacters
- e.g. asterisk ( * ) pathname globbing and some other metacharacters
- double quoting also preserves any special characters present whitespace in the text
- e.g. spaces, newlines (\n) or , and Tabs (\t)
- 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 ( ` )
- note that the syntax $(<expression>) is equivalent to `<expression>`
...
| Tip |
|---|
Always use single ( ' ) or double ( " ) quotes when you define an environment variable whose value contains spaces so that the shell sees the quoted text as one item. |
Single vs double quotes examplesExamples:
| Code Block | ||
|---|---|---|
| ||
echo "My Unix group is $MY_GROUP" # The text "$MY_GROUP" is evaluated # and its value substituted echo 'My Unix group is $MY_GROUP' # The text "$MY_GROUP" is left as-is foo="My USER name is $USER"; echo $foo # The text "$USER" is evaluated and its value # and its value substituted foo='My USER name is $USER'; echo $foo # The text "$USER" is left as-is FOO="Hello world!" echo "The value of variable 'FOO' is \"$FOO\"" # Escape the double quotes inside double quotes | ||
| Tip | ||
If you see the greater than ( > )
# inside double quotes
# Same output - the shell removes whitespace
echo Hello world!; echo Hello world!
# Echo a multi-line text variable without quotes, and the
# shell removes whitespace (here the newline)
foo='aa
bb'; echo $foo
# But enclose the multi-line variable in double quotes and whitespace
# is preserved
echo "$foo" |
| Tip |
|---|
If you see the greater than ( > ) character after pressing Enter, it can mean that your quotes are not paired, and the shell is waiting for more input to contain the missing quote of the pair (either single or double). Just use Ctrl-c to get back to the command prompt. |
...
| Code Block | ||
|---|---|---|
| ||
date # Calling the date command just displays 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 ); # replaces echothe $todaycommand # environmentAssign variablea "today"string isincluding assigned today's date to variable "today" 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
...
| 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 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 # sameTerminal place as# standardAnd out.this So data fromsyntax (2>&1) sends both standard output and standard error # to willthe same beplace writtenas tostandard outerrout.log ls haiku.txt xxx.txt 1>outerr.log 2>&1 |
...
- 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)