Giter Site home page Giter Site logo

bash-lab's Introduction

Bash lab

declare

declare a variable and set its attribute

declare -i

declare a integer

declare-i.sh

declare -i a
a="5+6"
echo $a
# => 12

declare -a

declare a index array

declare -n

declare -n nameref

declare-n.sh

declare -n name
anotherName="peter"
name=anotherName

# log anotherName or peter ?
echo $name

# => peter
# the value of anotherName variable
# declare -n name, change the interpretation of the right value to the name reference. 

# if we change the value of anotherName variable,
# will the value of name change?
anotherName="tom"
echo $name 

# => tom  change together!!!

declare -f

show the information about function

say() {
	echo "hello world"
}
declare -f say

use bc to perform arithmetic operations on floating-point numbers

use-bc.sh

echo "2+3" | bc
# bc accepts standard input to be evaluated, not parameters.

special parameters

special-parameters.sh

# $* 
# 1. epands to the positional parameters, staring from one.
# 2. when the expansion is not within double quotes, each positional
# parameter expands to a seperate word. $*
# 3.when the expansion is with double quotes, it expands to a single
# word with the value of each parameter seperated by the first letter of
# the IFS variable. "$*" is equivalent to "$1c$2$3...", where c is the
# first letter of the value of the IFS variable. 

# test
# caller "hello world" ok $(4+8)  

echo '$* wthin double quote'
for e in "$*"
do
	echo $e
done
# => 
# 

echo '$* not wthin double quote'
for e in $*
do
	echo $e
done




echo "**************************"



# $@ 
# 1. epands to the positional parameters, staring from one.
# 2. In context where word splitting is performed, this expands each positional
# parameter to a separate word; if not within double quote, those words are subject to
# word splitting.
# 3. In Context where world splitting is not performed, this expands to a single word
# with each potional parameter seperated by a space.
# test
# caller "hello world" ok $(4+8)  


echo '$@ with double quote'
for e in "$@"
do
	echo $e
done

# =>
# hello
# word
# ok
# 12



echo '$@ not with double quote'
for e in $@
do
	echo $e
done

variable scope

varaible-scope-in-function.sh

fun1() {
	name="peter";
	fun2;
}

fun2() {
	name="tom"
	echo "Hello $name"
}

name="simi"
fun1
# => Hello tom
echo "Hello $name"
# => Hello tom

# if we do not use local keyword to 
# define a variable in function, it will 
# have global scope.

# bash uses dynmaic scope.
 

assign

+=

add-equal-assign.sh

# += assign 

# string
name=peter
name+=" is 18"

echo $name
# => peter is 18


# index Array
declare -a array
idxArray=(peter is 18)
# How to output all array?
echo ${idxArray[*]}

# associated Array
declare -A assArray
assArray=([name]=peter [age]=18)
echo ${assArray[*]}

# number
declare -i n=10
n+=11

echo $n
# => 21 

what kind of operations can be performed on right-hand side value of an assignment operator

right-value-do-what-when-assign.sh

# right value undergo
#	tilde expansion ~
#	parameter and varaible expansion
#	commond substitution $()
#	arithmetic expansion $(())	
#	quote removal 

# dose not undergo
#	word splitting
#	filename 

homePath=~

echo "homePath: $homePath"
# => /root


anotherName=peter
name=$anotherName

echo "name: $name"
# => peter

# calculate the number of file in current directory
commandResult=$(ls | wc -l)
echo "commandResult(ls | wc -l): $commandResult" 

exp=$((4+5))
echo "exp(4+5): $exp"
# => 9


str="hello world"
echo "str: $str"
# => str: hello world

filenames=*

echo "filenames: $filenames"
# => *

funcation can be a parameter

function-as-parameter

function say(){
	$1;
}

function callback() {
	echo "Hello world"
}

say  callback 
# we can transmit a function name, and then execuate it. 

command expand

command-expand-then-word-splitting.sh

command-expand-then-word-splittin g.sh

say() {
	echo "hell word"
}
echoParameters() {
	echo $1
	echo $2
}
echo $(say)
# => hello world
# not word splitting
# as argument

for e in $(say)
do
	echo $e;
done
# word splitting 

comand expand return arithmetic

command-expand-return-arithmetic.sh

# generateArithmeticExpand
# echo peter{ok,no}
# 
generateArithmeticExpand() {
#	echo 'peter {ok,no}'
	echo '$((2+3))'
}


echo $(generateArithmeticExpand)
# => $((2+3))


for i in $(generateArithmeticExpand)
do	
	echo $i
done
# => $((2+3))

# not arithmetic expand

bash-lab's People

Contributors

perterhuan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.