Differences between revisions 1 and 2
Revision 1 as of 2023-01-30 01:49:56
Size: 1775
Comment:
Revision 2 as of 2023-01-30 02:05:41
Size: 1775
Comment:
Deletions are marked like this. Additions are marked like this.
Line 64: Line 64:
||`$@` ||all arguments as an [[Bash/Array|array]] ||
||`$*` ||all arguments as a string       ||
||`$@` ||all argument tokens                    ||
||`$*` ||all arguments as a single token ||

Bash Variables

For a list of special variables are used internally by the bash(1) shell, see here.


Declaring

Variables are declared with an equals sign (=).

a=1
b=2
c=3

Note that variables can be set to an empty value, and this is distrinct from not being set.

d=


Usage

Variables are accessed by their name. A dollar sign ($) must be prefixed to the name.

a=foo
b=$a       # 'foo'

To delimit a variable name from string literals, use braces. For example:

a=foo
b=${a}bar  # 'foobar'

If a variable's value includes a character that the shell will interpret specially, quote the variable.

a=foo
b="$a bar" # 'foo bar'


Special Variables

The following variables are set automatically by bash(1).

Variable

Value

$#

number of arguments

$@

all argument tokens

$*

all arguments as a single token

$?

the exit code

$$

the PID of the shell

$!

the PID of the most recent background job

Positional Variables

The name of a command is stored in $0.

The first argument to the command is stored in $1. And so on until the 9th argument, $9.

From the tenth argument, while arguments are stored in an indexed variable, they must be accessed differently. $10 is interpretted like ${1}0. To actually access the 10th argument, try ${10}.


CategoryRicottone

Shell/Variables (last edited 2023-01-30 02:07:28 by DominicRicottone)