Several alternative embodiment Shell parameters

When using a script, it is necessary to replace the parameter value to its simplest form is to add a dollar sign in front of the parameter, for example, $ior $9.
1 ${parameter}:
If the parameter name because the back of the characters may lead to a conflict, you can put the parameter name in braces, for example:

mv $file ${file}x

The command $ file specified file renamed after the original file name plus a x. This command can not be written as follows:

mv $file $filex

Because the value of the shell will filex as the second parameter.
To access the parameter is greater than or equal to 10 positions must be digital in braces, such as: ${11}.
2. ${parameter:-value}
This structure means that, if the parameter is not empty on its value, otherwise it is replaced with a value. E.g:

echo Using editor ${EDITOR:-/bin/vi}

If EDITOR is not empty, shell will be replaced with its value, otherwise it is replaced with the value of / bin / vi. It is the same effect as the script:

if [ -n "$EDITOR" ]
then
    echo Using editor $EDITOR
else
    echo Using editor /bin/vi
fi

In this test on the terminal structure is as follows:

[root@localhost-test ~]#EDITOR=/bin/ed
[root@localhost-test ~]#echo ${EDITOR:-/bin/vi}
/bin/ed
[root@localhost-test ~]#EDITOR=
[root@localhost-test ~]#echo ${EDITOR:-/bin/vi}
/bin/vi
[root@localhost-test ~]#

3 ${parameter:=value}
This is a structure similar to the previous, except that: When the parameter is empty, not only use value, it is also assigned to the parameter. But not in this way to position parameter assignment (ie parameter can not be a number).
A typical use of this structure is full of variables to test whether a value is set, and if not it will be set to the default value, like:

${$PHONEBOOK:=$HOME/phonebook}

It means that if PHONEBOOK has been set to a value, that value is retained, otherwise it will be set to $ HOME / phonebook.
Note that the preceding examples do not as a separate command, because the shell after the completion of the replacement attempts to perform the alternative results:

[root@localhost-test ~]#PHONEBOOK=
[root@localhost-test ~]#${PHONEBOOK:=$HOME/phonebook}
-bash: /root/phonebook: No such file or directory
[root@localhost-test ~]#

If such a structure should be used as a separate command, often by means of a null command. If you write

: ${PHONEBOOK:=$HOME/phonebook}

replacement shell still do, but nothing is implemented.

[root@localhost-test ~]#PHONEBOOK=
[root@localhost-test ~]#: ${PHONEBOOK:=/HOME/phonebook}
[root@localhost-test ~]#

4 ${parameter:?value}
If the parameter is not empty, shell replaces its value, otherwise the shell put the value written to standard error, then exit (this happens in the shell landed, will not log out from the system).
If you omit the value, shell to write information:

prog:parameter null or not set

The following is an example on the terminal:

[root@localhost-test ~]#PHONEBOOK=
[root@localhost-test ~]#: ${PHONEBOOK:?"NO PHONEBOOK file"}
-bash: PHONEBOOK: NO PHONEBOOK file
[root@localhost-test ~]#: ${PHONEBOOK:?}
-bash: PHONEBOOK: parameter null or not set
[root@localhost-test ~]#

With this structure can easily check whether a program requires a set of variables are set up and are not empty, as:

: ${TOOLS:?} ${EXPTOOLS:?} ${TOOLBIN:?}

5 ${parameter:+value}
If the parameter is not empty, then this structure would be replaced value, nothing else replaced.

[root@localhost-test ~]#PHONEBOOK=1
[root@localhost-test ~]#echo option : ${PHONEBOOK:+"phonebook"}
option : phonebook
[root@localhost-test ~]#PHONEBOOK=
[root@localhost-test ~]#echo option: ${PHONEBOOK:+"phonebook is empty"}
option:
[root@localhost-test ~]#echo :$PHONEBOOK:
::
[root@localhost-test ~]#
Published 46 original articles · won praise 13 · views 60000 +

Guess you like

Origin blog.csdn.net/luliuliu1234/article/details/80994383