Quoting&Parameters

Definitions

<blank>:A space or tab character.
<word>:A sequence of characters treated as a unit by the shell.
<name>:
A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore.
Names are used as shell variable and function names.
<token>:
A sequence of characters considered a single unit by the shell. It is either a word or an operator.

Quoting

//移除某些字符或单词的特殊含义
Quoting is used to remove the special meaning of certain characters or words to the shell.
There are three quoting mechanisms: the escape character, single quotes, and double quotes.

//Escape Character
A non-quoted backslash '\' is the Bash escape character.
It preserves the literal value of the next character that follows, with the exception of newline.
If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation(that is,
it is removed from the input stream and effectively ignored).

//Single Quotes
Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes.
A single quote may not occur between single quotes, even when preceded by a backslash.

//Double Quotes
Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the
exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled,!.
The characters ‘$’ and ‘`’ retain their special meaning within double quotes(see Shell Expansions).
The backslash retains its special meaning only when followed by one of the following characters:‘$’, ‘`’, ‘"’, ‘\’, or newline.
A double quote may be quoted within double quotes by preceding it with a backslash.
The special parameters ‘*’ and ‘@’ have special meaning when in double quotes(see Shell Parameter Expansion).
echo \n#n
echo \nbr#nbr
echo \\nbr#\nbr
echo \\\\#\\(前两个\变为一个\,后两个\变为一个\)

echo '\mbr'#\mbr
echo '\\mbr'#\\mbr
echo '"\\\kkll"'#"\\\kkll"
echo '$PATH'#$PATH

#\mbr
echo "\mbr"
#\mbr
echo "\\mbr"
#"mb"   ve\yhhj
echo "\"mb\"   ve\yhhj"
#"grep" command not found.
echo "\"grep\" command not found."
#res: 'location is /usr/local/src/jdk1.8.0_333'
#可见,先进行Expansion,然后是Quote Removal.
echo "res: 'location is $JAVA_HOME'"

Shell Parameters

A parameter is an entity that stores values.
It can be a name, a number, or one of the special characters listed below under Special Parameters.

A variable is a parameter denoted by a name.
A variable has a value and zero or more attributes.
Attributes are assigned using the declare builtin command.
Once a variable is set, it may be unset only by using the unset builtin command.

A variable may be assigned to by a statement of the form
name=[value]#=左右不要有空格
If value is not given, the variable is assigned the null string.
All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and
quote removal (see Shell Parameter Expansion). 
If the variable has its integer attribute set,then value is evaluated as an arithmetic expression even
if the $(()) expansion is not used.
Assignment statements may also appear as arguments to the alias, declare, typeset, export, readonly,
and local builtin commands.
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0.
Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using
the set builtin command.
Positional parameters may not be assigned to with assignment statements.
The set and shift builtins are used to set and unset them.
The positional parameters are temporarily replaced when a shell function is executed(see Shell Functions).
When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces.
Special Parameters
The shell treats several parameters specially.These parameters may only be referenced;assignment to them is not allowed.

$,($$)
#Expands to the process ID of the shell.
#In a subshell, it expands to the process ID of the current shell, not the subshell.
-,($-)
#Expands to the current option flags as specified upon invocation, by the set builtin command, or those
#set by the shell itself(such as the -i option).
*,($*)
#When the expansion is not within double quotes, each positional parameter expands to a separate word.
#In contexts where it is performed, those words are subject to further word splitting and filename expansion.
#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.
@,($@)
#In contexts where word splitting is performed,this expands each positional parameter to a separate word;
#if not within double quotes, these words are subject to word splitting.
0,($0)
#Expands to the name of the shell or shell script.This is set at shell initialization.
#If Bash is invoked with a file of commands, $0 is set to the name of that file.
#If Bash is started with the -c option,then $0 is set to the first argument after the string to be executed,if one is present.
#Otherwise,it is set to the filename used to invoke Bash,as given by argument zero.
#当$0是-bash时,表示此时是login shell,也可以通过shopt命令查看
#,($#)
#Expands to the number of positional parameters in decimal.

例子

#test.sh
echo '$_是:' "$_"
echo '$0是:' "$0"
echo '$$是:' "$$" ',$BASHPID是:' "$BASHPID"
echo '$#是:' "$#"
echo '$-是:' "$-"
for var in $* #注意$*,带不带引号是不一样的
do
	echo "==$var=="
done
[root@kafka100 cpp]# ./temp/test.sh 1 2 '3 4 gh dff4'
$_是: ./temp/test.sh
$0是: ./temp/test.sh
$$是: 3835 ,$BASHPID是: 3835
$#是: 3
$-是: hB
==1==
==2==
==3==
==4==
==gh==
==dff4==
#将for循环改为for var in "$*",结果变为
==1 2 3 4 gh dff4==
#将for循环改为for var in $@,结果变为
==1==
==2==
==3==
==4==
==gh==
==dff4==
#将for循环改为for var in "$@",结果变为
==1==
==2==
==3 4 gh dff4==
All of the single-character options used with the set builtin can be used as options when the shell is invoked.
bash [options] [command_string | file]
-c
#Read and execute commands from the first non-option argument command_string, then exit.
#If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments
#are assigned to the positional parameters.
#例子
[root@kafka100 cpp]# bash -c 'echo $0==$1==$2' dfm hhj kks
dfm==hhj==kks
[root@kafka100 cpp]# bash -c 'echo ==$@==' dfm hhj kks
==hhj kks==
[root@kafka100 cpp]# bash -c 'eho ==$@==' dfm hhj kks
dfm: eho: command not found
[root@kafka100 cpp]# eho
bash: eho: command not found

https://www.gnu.org/software/bash/manual/bash.html
https://www.baeldung.com/linux/dollar-star-at

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值