@rkenmi - Bash Syntax, Operators and Notations

Bash Syntax, Operators and Notations


"Good to know" syntax, operators and notations for Bourne-Again shell scripting and/or finding your way around the terminal.

Bash Syntax, Operators and Notations


Back to Top

Updated on May 28, 2019

Double Dash

Used to signify the end of command options, after which only positional arguments are taken.

For example, you may want to create an odd file named -abc. touch -abc will not result in the creation of a new file named -abc, however. It will instead try to interpret the added -abc as a parameter option, which is contrary to what you want. To get around this, run touch -- -abc.


TLDR Cheat Sheet:

-- = Used to signify the end of command options.

> = Redirect stdout

1> = Redirect stdout (equivalent to above). The 1 is a file descriptor.

2> = Redirect stderr

>& = Redirect a stream to another file descriptor.

2>&1 = Redirect 2 (stderr) to 1 (stdout).

| = Pipe operator; pipe the output and pass that output as input for another command.

a & b = Run a in the background and run b in the current shell.

a && b = Run a. If a succeeds, run b.

a || b = Run a. If a fails, run b. (If a succeeds every time, b is never run.) . Not related to the pipe operator or |.

${var} = Expands a variable

$(cmd) = Evaluate a command inside the parentheses. Command substitution. The modern and preferred way of doing command substitution.

`something` = Everything inside the backtick is evaluated before. An older way of doing command substitution. This method doesn't allow you to nest expressions like $(cmd) can. Another caveat is that this method only substitutes the stdout, and not stderr.


WIP


Article Tags:
unixbashterminalwikiunlisted