bash

[!NOTE|label:references:]

fancy bash

[!NOTE]

  • get bash login log ( for rc script debug )

  • run with only one startup file ( for sharing accounts )

array

[!NOTE|label:references:]

sort array

[!NOTE|label:references:]

  • sample array:

NAME
EXAMPLE

Brace Expansion

echo a{d,c,b}e

Tilde Expansion

~

Shell Parameter Expansion

string=01234567890abc; echo ${string:7:2}

Command Substitution

$(command) or command

Arithmetic Expansion

$(( expression ))

Process Substitution

<(list) or >(list)

Word Splitting

$IFS

Filename Expansion

*, ? , [..],...

IFS

[!NOTE]

  • or

  • example

    while read show variable

Bash scans each word for the characters '*', '?', and '[', unless the -f (set -f) option has been set

CONDITION
RESULT

match found && nullglob disabled

the word is regarded as a pattern

no match found && nullglob disabled

the word is left unchanged

no match found && nullglob set

the word is removed

no match found && failglob set

show error msg and cmd won't be exectued

nocaseglob enabled

patten match case insensitive

set -o noglob or set -f

* will not be expanded

shopt -s dotglob

* will including all .*. see zip package with dot-file

sample:

#
Expression
Result
Comments

1

"$a"

apple

variables are expanded inside ""

2

'$a'

$a

variables are not expanded inside ''

3

"'$a'"

'apple'

'' has no special meaning inside ""

4

'"$a"'

"$a"

"" is treated literally inside ''

5

'\''

invalid

can not escape a ' within ''; use "'" or $'\'' (ANSI-C quoting)

6

"red$arocks"

red

$arocks does not expand $a; use ${a}rocks to preserve $a

7

"redapple$"

redapple$

$ followed by no variable name evaluates to $

8

'\"'

\"

\ has no special meaning inside ''

9

"\'"

\'

\' is interpreted inside "" but has no significance for '

10

"\""

"

\" is interpreted inside ""

11

"*"

*

glob does not work inside "" or ''

12

"\t\n"

\t

and have no special meaning inside "" or ''; use ANSI-C quoting

13

"echo hi"

hi

`` and $() are evaluated inside "" (backquotes are retained in actual output)

14

'echo hi'

echo` hi

`` and $() are not evaluated inside '' (backquotes are retained in actual output)

15

'${arr[0]}'

${arr[0]}

array access not possible inside ''

16

"${arr[0]}"

apple

array access works inside ""

17

$'$a\''

$a'

single quotes can be escaped inside ANSI-C quoting

18

"$'\t'"

$'\t'

ANSI-C quoting is not interpreted inside ""

19

'!cmd'

!cmd

history expansion character '!' is ignored inside ''

20

"!cmd"

cmd args

expands to the most recent command matching "cmd"

21

$'!cmd'

!cmd

history expansion character '!' is ignored inside ANSI-C quotes

ternary arithmetic

[!NOTE]

  • string

  • mathematical operation

scp multipule folder/file to target server

fast copy or moving or something (detials -> brace expansion)

  • example 1:

  • example 2

  • example 3

  • example 4

multiple directories creation

copy single file to multipule folders

pipe and stdin

  • to multiple variables

  • to array

  • every single char to array including spaces

    [!TIP]

    • tricky of sed

read stdin from pipe

[!TIP]

read -r var ( for command | trim )

option
expression

!

start a history substitution

!n

refer to command line n

!-n

refer to the command n lines back

!!

refer to the previous command

!string

refer to the most recent command preceding the current position in the history list starting with string

!?string[?]

refer to the most recent command preceding the current position in the history list containing string.

^string1^string2^

!!:s^string1^string2^ quick substitution. repeat the last command, replacing string1 with string2

!#

the entire command line typed so far

option
expression

!!

designates the preceding command

!!:$ or !$

designates the last argument of the preceding command

!fi:2

designates the second argument of the most recent command starting with the letters fi

$_ VS. !$

reference:

-$_

  • if the invoking application doesn't pass a _ environment variable, the invoked bash shell will initialise $_ to the argv[0] it receives itself which could be bash

  • i.e.

  • !$

CHARACTER
DEFINITION
EXAMPLE

~

$HOME

~/foo: $HOME/foo

~+

$PWD

~+/foo: $PWD/foo

~N

dirs +N

-

~+N

dirs +N

-

~-N

dirs -N

-

CHARACTER
DEFINITION

$*

expands to the positional parameters, starting from one. when the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the ifs special variable.

$@

expands to the positional parameters, starting from one. when the expansion occurs within double quotes, each parameter expands to a separate word.

$#

expands to the number of positional parameters in decimal.

$?

expands to the exit status of the most recently executed foreground pipeline.

$-

a hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).

$$

expands to the process id of the shell.

$!

expands to the process id of the most recently executed background (asynchronous) command.

$0

expands to the name of the shell or shell script.

$_

the underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. subsequently, it expands to the last argument to the previous command, after expansion. it is also set to the full pathname of each command executed and placed in the environment exported to that command. when checking mail, this parameter holds the name of the mail file.

$* vs. $@:

  • The implementation of "$*" has always been a problem and realistically should have been replaced with the behavior of "$@".

  • In almost every case where coders use "$*", they mean "$@".

  • "$*" Can cause bugs and even security holes in your software.

Last updated

Was this helpful?