[LINUX] Bash script memo

bash memo

I made a script for updating IPv4 of MyDNS.JP with a shell script (MyDNS.jp-v4-Update.sh). , Note the findings at that time

Variable assignment

Substitute without opening a space

hoge="hoge"

NG example

If you open a space and substitute as shown below, you will get angry with not found or command not found.

hoge = "hoge"

Get string length

It seems that it can be taken as a numerical value

${#hoge}

String variable expansion

Put a dollar on your head

echo $hoge

String concatenation

ʻEcho" Moji $ {hoge} Moji "`

Command expansion

Part 1

Enclose in backticks

hoge=`echo "hoge"`

Part 2

Enclose in $ (). Easy to write when nesting

hoge=$(echo "hoge")

Nest sample connecting 1 and 2

For example, the content of ip_res will be something like " 127.0.0.1 200 "

ip_res=echo """$(curl inet-ip.info/ip -s -m 10 -w ' %{http_code}')"""



## Get date

`` hoge_date=`date "+%Y/%m/%d %H:%M:%S"` ``  

## if statement

```sh
hoge="hoge"
if [ ${hoge} = "hoge" ]; then
  #Honyara
elif [ 1 -eq 1 ]; then
  #Are comparison operators different for numbers and strings?
else
  #Funyara
fi

function

There seems to be no concept of scope unless explicitly specified


fuga() {
  #Fugarara
}

fuga

Escape sequence with echo

Add the -e option

echo -e "\n"

Reference article

-Operators used in if and while statements in shell scripts (bash) -Note on Bash variable scope

Recommended Posts

Bash script memo
DynamoDB Script Memo (Python)
gzip memo
Raspberry-pi memo
Pandas memo
HackerRank memo
Python memo
python memo
graphene memo
pyenv memo
Matplotlib memo
pytest memo
sed memo
Python memo
Install Memo
BeautifulSoup4 memo
tomcat memo
Generator memo.
psycopg2 memo
Python memo
SSH memo
Memo: rtl8812
Let's run a Bash script in Java
pandas memo
Shell memo
Python memo
Pycharm memo
Python memo
docker alpine bash git prompt completion setting memo
[Self memo] Let's enjoy using alias with bash