- fancy bash
- alias
- shell expansions
- quoting
- ternary arithmetic
- brace expansion
- pipe and stdin
- event designators
- tilde expansion
- special parameters
[!NOTE] references:
- Bash scripting cheatsheet
- Introduction to Bash Array
Bash For Loop Array: Iterate Through Array Values
$ declare -A foo=( ["one"]="apple" ["two"]="orange" ["three"]="banana" ) # show keys $ echo ${!foo[@]} two three one # show values $ echo ${foo[@]} orange banana apple
fancy bash
[!NOTE]
alias
$ echo ${BASH_ALIASES[ls]}
ls --color=always
bash -<parameter>
get bash login log ( for rc script debug )
$ bash -l -v
run with only one startup file ( for sharing accounts )
$ bash -i --rcfile="$HOME/.marslo/.imarslo"
shell expansions
references:
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]
# default IFS
$ echo "${IFS@Q}"
$' \t\n'
$ echo "$IFS" | od -tcx1
0000000 \t \n \n
20 09 0a 0a
0000004
$ echo -n "$IFS" | od -tcx1
0000000 \t \n
20 09 0a
0000003
# i.e.:
$ read a b c <<< "foo bar baz"; echo $a - $b - $c
foo - bar - baz
or
$ cat -c -etv <<<"$IFS" ^I$ $ $ printf "%s" "$IFS" | od -to1 -vtc 0000000 040 011 012 \t \n 0000003
example
$ IFS=' ' read -p 'Enter your first and last name : ' first last; echo ">> hello $first $last" Enter your first and last name : marslo jiao >> hello marslo jiao # read from array $ foo=( x=y a=b ) $ while IFS='=' read -r var value; do echo "$var >> $value"; done < <(printf '%s\n' "${foo[@]}") x >> y a >> b
word splitting
# due to 7 fields are spitted via `:` in /etc/passwd
IFS=':' read f1 f2 f3 f4 f5 f6 f7 < /etc/passwd
filename expansion
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 |
quoting
sample:
a=apple # a simple variable
arr=(apple) # an indexed array with a single element
# | 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\n |
\t and \n 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
$ [[ '.' = '.' ]] && path='.' || path='--' $ echo $path . $ [[ '.' = '-' ]] && path='.' || path='--' $ echo $path --
mathematical operation
$ (( 3 == 3 ? (var=1) : (var=0) )) $ echo $var 1 $ (( 3 == 1 ? (var=1) : (var=0) )) $ echo $var 0
brace expansion
segmented continuous
# exclude 7 from 1-10
$ echo test-{{1..6},{8..10}}
test-1 test-2 test-3 test-4 test-5 test-6 test-8 test-9 test-10
scp multipule folder/file to target server
$ scp -r $(echo dir{1..10}) user@target.server:/target/server/path/
all about {curly braces} in bash
$ echo 00{1..9} 0{10..99} 100
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100
$ dec2bin=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
$ echo ${dec2bin[1]}
00000001
$ echo ${dec2bin[0]}
00000000
$ echo ${dec2bin[255]}
11111111
$ month=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
$ echo ${month[5]}
Jun
$ echo {10..0..2}
10 8 6 4 2 0
$ echo {1..100..3}
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
fast copy or moving or something (detials -> brace expansion)
- Example 1:
$ ls | grep foo $ touch foo{1,2,3} $ ls | grep foo foo1 foo2 foo3
Example 2
$ ls | grep foo $ touch foo-{a..d} $ ls | grep foo foo-a foo-b foo-c foo-d
Example 3
$ ls foo-* foo-a foo-b foo-c foo-d $ mv foo-{a,} $ ls foo-* foo- foo-b foo-c foo-d
Example 4
$ mkdir -p test/{a,b,c,d} $ tree test/ test/ ├── a ├── b ├── c └── d 4 directories, 0 files
multiple directories creation
$ mkdir sa{1..50}
$ mkdir -p sa{1..50}/sax{1..50}
$ mkdir {a-z}12345
$ mkdir {1,2,3}
$ mkdir test{01..10}
$ mkdir -p `date '+%y%m%d'`/{1,2,3}
$ mkdir -p $USER/{1,2,3}
copy single file to multipule folders
$ echo dir1 dir2 dir3 | xargs -n 1 cp file1
# or
$ echo dir{1..10} | xargs -n 1 cp file1
pipe and stdin
read stdin from pipe
references:
- How to read mutliline input from stdin into variable and how to print one out in shell(sh,bash)?
- In a bash function, how do I get stdin into a variable
- TIL: Reading stdin to a BASH variable
- Pipe Output to Bash Function
- Guide to Stream Redirections in Linux
- Bash: Assign output of pipe to a variable
- read
- How To Use The Bash read Command
- $IFS
IFS Effect On The Values of "$@" And "$*": $@ and $* are special command line arguments shell variables. The $@ holds list of all arguments passed to the script. The $* holds list of all arguments passed to the script.
[!TIP]
# with IFS $ echo ' hello world ' | { IFS='' read msg; echo "${msg}"; } | tr ' ' '.' ...hello..world... $ echo ' hello world ' | { IFS='' read msg; echo "${msg}" | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'; } | tr ' ' '.' hello..world # without IFS $ echo ' hello world ' | { read msg; echo "${msg}"; } | tr ' ' '.' hello..world
read -r var
script as command line
$ cat trim.sh #!/usr/bin/env bash trim() { echo "$@" | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//' } IFS='' read -r myvar trim "${myvar}"
- result
$ IFS='' $ s=' aa bb ' $ echo "${s}" | tr ' ' '.' # ...aa..bb... $ echo "${s}" | ./trim.sh | tr ' ' '.' # aa..bb $ echo " a | b | c " | awk -F'|' '{print $2}' | tr ' ' '.' # .b. $ echo " a | b | c " | awk -F'|' '{print $2}' | ./trim.sh | tr ' ' '.' # b
- result
running inside the script
$ cat example.sh #!/usr/bin/env bash trim() { IFS='' read -r str echo "${str}" | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//' } s=' aa bb ' echo "${s}" | tr ' ' '.' echo "${s}" | trim | tr ' ' '.'
- result
$ ./example.sh ...aa..bb... aa..bb
- result
event designators
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 |
word designators
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:
-$_
- bash variables
- 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.
$ env | grep '^_=' _=/usr/local/opt/coreutils/libexec/gnubin/env # or $ env bash -c 'echo "$_"' /usr/local/opt/coreutils/libexec/gnubin/env
!$
- Word Designators
- equal to
!!:$
tilde expansion
CHARACTER | DEFINITION | EXAMPLE |
---|---|---|
~ |
$HOME |
~/foo : $HOME/foo |
~+ |
$PWD |
~+/foo : $PWD/foo |
~N |
dirs +N |
- |
~+N |
dirs +N |
- |
~-N |
dirs -N |
- |
# prepare
$ mkdir -p a/b/c/d
$ cd a && pushd .
$ cd b && pushd .
$ cd c && pushd .
$ cd d && pushd .
# result
$ dirs -v
0 ~/a/b/c/d
1 ~/a/b/c/d
2 ~/a/b/c
3 ~/a/b
4 ~/a
$ echo $(dirs -1)
~/a/b
$ echo $(dirs -2)
~/a/b/c
$ echo $(dirs -3)
~/a/b/c/d
special parameters
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.