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:
sort array by key
[!TIP] !! highly recommend !!
references:
NAME | EXAMPLE |
---|---|
Brace Expansion |
|
Tilde Expansion |
|
Shell Parameter Expansion |
|
Command Substitution |
|
Arithmetic Expansion |
|
Process Substitution |
|
Word Splitting |
|
Filename Expansion |
|
IFS
[!NOTE]
or
example
Bash scans each word for the characters
'*'
,'?'
, and'['
, unless the-f
(set -f
) option has been set
CONDITION | RESULT |
---|---|
match found && | the word is regarded as a pattern |
no match found && | the word is left unchanged |
no match found && | the word is removed |
no match found && | show error msg and cmd won't be exectued |
| patten match case insensitive |
|
|
|
|
sample:
# | Expression | Result | Comments |
---|---|---|---|
1 |
|
| variables are expanded inside |
2 |
|
| variables are not expanded inside |
3 |
|
|
|
4 |
|
|
|
5 |
| invalid | can not escape a |
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
| glob does not work inside |
12 |
|
| and have no special meaning inside |
13 |
|
|
|
14 |
|
|
|
15 |
|
| array access not possible inside |
16 |
|
| array access works inside |
17 |
|
| single quotes can be escaped inside ANSI-C quoting |
18 |
|
| ANSI-C quoting is not interpreted inside |
19 |
|
| history expansion character |
20 |
|
| expands to the most recent command matching |
21 |
|
| history expansion character |
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
references:
[!TIP]
read -r var
( for command | trim
)
read -r var
( for command | trim
)script as command line
result
running inside the script
result
option | expression |
---|---|
| start a history substitution |
| refer to command line n |
| refer to the command n lines back |
| refer to the previous command |
| refer to the most recent command preceding the current position in the history list starting with string |
| refer to the most recent command preceding the current position in the history list containing string. |
|
|
| the entire command line typed so far |
option | expression |
---|---|
| designates the preceding command |
| designates the last argument of the preceding command |
| 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.
!$
equal to
!!:$
CHARACTER | DEFINITION | EXAMPLE |
---|---|---|
|
|
|
|
|
|
|
| - |
|
| - |
|
| - |
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. |
| 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