Shell script learning log - output, variables, strings

1. Run the first program, Hello world

  • Create a .sh file
  • write code
  • Modify permissions
  • run

#!/bin/bash
echo "hello world"

Two, echo printing method

        In the shell, you can use single quotes, double quotes, and do not apply any symbols to modify the content to print.

#!/bin/bash
echo "echo 的三种输出方式"

echo 'tom'
echo tom
echo "tom"
echo tom

3. Variables

  • Variables can be customized in the shell without modification.
  • To use a variable, precede the variable name with a dollar sign.
  • The curly braces outside the variable name are optional, mainly to facilitate the distinction.
  • The unset command can delete variables

#!/bin/bash
echo '------------变量---------------'
#=中间不可有空格
name="tom"
echo $name
echo ${name}
echo '123 $name'
echo "123 $name"
unset name
echo $name

4. String

  • Use # to get the string length
  • ${str:1:3} intercepts the first to third characters of the string
  • `expr index` find string

        

echo "-----------字符串-----------"
str="advad"
echo $str
echo "str 长度为:${#str}"
echo ${str:1:3}
la=${str:2:4}
echo la:$la
echo "索引 la"
echo `expr index $la a`

Guess you like

Origin blog.csdn.net/qq_53734051/article/details/126448229