An important feature of shell scripts is the possibility of confirming execution with success of a given command.
It’s called a return variable.
The return variable is filled every time a command is executed. If the value returned is 0, the command was successfully executed.
If the value is different from 0, a problem was encountered.
If used in scripts, its value must be verified immediately after the execution of the desired command.
Variable | What is for |
$# | Returns the number of arguments the program received. |
$_ | The underscore contains the absolute name of the shell file or script that is being executed as passed in the argument list. |
$? | Return variable when a program terminates execution. |
$$ | Provides the PID of the Shell process. |
$! | Provides the PID of the last program running in the background. |
$0 | Returns the name of the program executed. |
$n | returns the program’s n-number argument. |
$* $@ | Returns all arguments entered when running the program. |
Take a look at the sample script where these variables are used:
#! /bin/bash
echo “My name is: $0"
echo “I have $# arguments passed in my execution”
echo “This is the result of the last execution: $_”
echo “The first argument is: $1"
echo “The second argument is: $2"
echo “The third argument is: $3"
echo “My PID is: $”
echo “My arguments are: $@”
echo “My arguments are: $*”
When executed, it will produce the following result:
$./script one two three My name is:. /script I have 3 arguments passed in my execution This is the result of the last execution: I have 3 arguments passed in my execution The first argument is: one The second argument is: two The third argument is: three My PID is: 6210 My arguments are: one two three My arguments are : one two three
The variable $? represents the exit status of the previous command.
Example:
$ echo “Uira” Uira $ echo $? 0
In this example the variable $? It will show the return code from the previous command echo “Will join”.
It has been agreed that when a command is executed without errors, it will return the return code 0 (zero). When there is an error, a value greater than 1 will be returned, and depending on the value, the command developer may provide a list of error codes and their meanings.
The variable $! will return the process ID of the last background command:
$ ls & [1] 27931
$ echo $! 27931
Don’t confuse it with the variable $$
The $$ variable shows the current shell process ID. For shell scripts, this is the ID of the process in which they are running:
$ echo $ 16487
$ ps -ef|grep 16487 16487 8098 0 June/21 pts/18 00:00:01 bash
The $# variable returns the number of arguments the program received. It can be used in scripts when you want to know if the number of arguments required has been satisfied.
Keys can be used to highlight to the shell a variable that is used “inside” a text. See the example:
$ x="Tri” $ y="${x}angle” $ echo $y Triangle
If you’re writing a shell script, it’s very important that you know the special variables.
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.