Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell parameter transfer

Shulou Source: shulou.com Published: 2022-06-02 06:40:50 10月06日 Update

We all know that ordinary bash commands can be followed by arbitrary parameters, so do our own scripts also support passing parameters? Of course the answer is yes.

Execute "vim test.sh" to create a new shell script. The script test.sh is as follows:

#! / bin/sh

Name=$1

Echo "the ${name} are great man!"

Assign executable permissions to the script of the newly created test.sh with the command "chmod + x test.sh"

Execute the command'. / test.sh "xiao wang" and you can see the result of your own script "the xiao wang are great man!"

In "name=$1", $1 is the location parameter provided by the system, $0 represents the name of the program (. / test.sh), [$1 $2.] Parameters that are passed starting at 1. In addition to location parameters, the linux system also provides built-in parameters, all of which are as follows:

$0-current script file name

A parameter passed to a script or function, where n is a number indicating the parameter, $1 represents the first parameter, and $2 represents the second parameter.

$#-the number of parameters passed to the script or function

$*-all parameters passed to a script or function, and when it is enclosed in double quotes (""), "$*" takes all parameters as a whole and outputs all parameters in the form of "$1 $2. $n"

$@-all parameters passed to a script or function, when enclosed in double quotation marks (""), are slightly different from $*, "$@" separates each parameter with "$1"$2"... Output all parameters in the form of "$n"

When $* and $@ are not contained in double quotes (""), they both use "$1" and "$2". Output all parameters in the form of "$n"

Let's write a Mini Program to practice $* and $@

$? the exit status of the previous command, or the return value of the function. If it exits normally, it returns 0, otherwise it is a non-zero value.

$$- the current shell process pid. For shell scripts, it is the process ID where these scripts are located

$!-pid of the previous command

Create a new test.sh chmod + x test.sh to execute. / test.sh "a"b"c"d" to observe the print result

#! / bin/bash

Echo "\ $* =" $*

Echo "\ $@ =" $@

Echo "print each param from\ $*"

For var in $*

Do

Echo $var

Done

Echo "print each param from\ $@"

For var in $@

Do

Echo $var

Done

Echo "print each param from\"\ $*\ ""

For var in "$*"

Do

Echo $var

Done

Echo "print each param from\"\ $@\ ""

For var in "$@"

Do

Echo $var

Done

Print the results:

$* = a b c d

$@ = a b c d

Print each param from $*

A

B

C

D

Print each param from $@

A

B

C

D

Print each param from "$*"

A b c d

Print each param from "$@"

A

B

C

D

Tags: Parameter script function command quotation mark form result output location program system process different ordinary number representative content name is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Shulou Tech Info NVidia Shulou Information