Introduction
Most people have used terminal command lines. Sometimes we need to repeatedly enter the same commands, so we can write our own scripts to automate this work.
Specifying the Interpreter
#!/bin/bash
This line specifies bash as the interpreter for our script. If we’re writing in Python syntax, we should change it to Python.
Defining Variables
variable_name=value For example: name=“hahaha” For ordinary characters, adding double quotes or single quotes doesn’t matter. name=hahaha also works.
Using Variables
Add $ before a variable name to use its value. This means to evaluate the symbol’s value.
Strings
Quotes have the following characteristics:
- Spaces act as word separators
- Words in single quotes
- Character pattern matching
- Pathname expansion
- Process substitution (redirection)
Single quotes won’t process the content further, while double quotes will evaluate the content. You can see the difference between echo “$name” and echo ‘$name’ to understand this.
String concatenation is just placing two strings together. ${#string} is used to get the string length, ${string:1:4} is used for slicing.
Getting Input
read inputstr read -p “please input : " inputstr The above two lines get input content from standard input.
Getting Script Execution Results
For example, to get the output of a Python script: into=$(‘python3’ ‘pyscript.py’ ‘arg1’ ‘arg2’) arg1 and arg2 are parameters